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 version="1.0" encoding="utf-8"?> <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.
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"; @Override 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 @Override 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 version="1.0" encoding="utf-8"?> <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.
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 { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onViewCreated(@NonNull View v, Bundle savedInstanceState) { super.onViewCreated(v, savedInstanceState); } @Override 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.