This post describes how to toggle between normal mode and fullscreen immersive mode using immersive mode fragment.
Create your layout in activity_main.xml.
XML
x
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:padding="10dp"
android:textSize="25dp" />
</LinearLayout>
In MainActivity.java use following code.
Java
package com.example.dialog;
import android.os.Bundle;
import android.view.Menu;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity {
public static final String FRAGTAG = "ImmersiveModeFragment";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Add ImmersiveModeFragment to FragmentTransaction
if (getSupportFragmentManager().findFragmentByTag(FRAGTAG) == null ) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
ImmersiveModeFragment fragment = new ImmersiveModeFragment();
transaction.add(fragment, FRAGTAG);
transaction.commit();
}
}
// Create options menu with res/menu/main.xml
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
In res/ folder create a menu/ folder, and in res/menu/ folder create main.xml. In res/menu/main.xml add a menu item Toggle Immersive Mode.
XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/sample_action"
app:showAsAction="never"
android:title="Toggle Immersive Mode" />
</menu>
Create a java class file ImmersiveModeFragment.java and put following codes in it.
Java
package com.example.dialog;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
public class ImmersiveModeFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public void onViewCreated( View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
}
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.sample_action) {
toggleFullscreen();
}
return true;
}
public void toggleFullscreen() {
int newUiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility();
newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE;
getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
}
}
Now run the app. It will show the toggle button in options menu, which can be used to toggle between fullscreen mode and normal mode.
