22

I want to make an activity to go into IMMERSIVE mode and hide top and buttom system bars as soon as it starts.

In developers site of android they say I should use setSystemUiVisibility() and provide SYSTEM_UI_FLAG_IMMERSIVE and SYSTEM_UI_FLAG_HIDE_NAVIGATION.

How can I do this in the OnCreate() method of the activity? I think the setSystemUiVisibility is not provided in the Activity class and it should happen in a view. Is there a workaround?

UPDATE

ok According to doorstuck I added the following lines but I dont see any changes, navigation bar and buttom buttons are still visible :

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
        }
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE);
    }

    //Rest of activity code
manlio
  • 18,345
  • 14
  • 76
  • 126
Dumbo
  • 13,555
  • 54
  • 184
  • 288

6 Answers6

47

Get the decor view:

getWindow().getDecorView().setSystemUiVisibility(...)

Remember that the arguments are bit flags. Only call the method above once:

getWindow().getDecorView().setSystemUiVisibility(
          View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_FULLSCREEN
        | View.SYSTEM_UI_FLAG_IMMERSIVE);
Alexis
  • 23,545
  • 19
  • 104
  • 143
doorstuck
  • 2,299
  • 1
  • 22
  • 29
  • Thanks, but can you give an example how to implement this? please see my update to the question. – Dumbo Jan 15 '14 at 12:38
  • 3
    Is it possible to set all this Flag from an Android style so I do not have to call it from my Activity? So I can set different styles for different versions from Android? – Cilenco May 14 '14 at 07:44
  • 3
    My app uses webview so I had to use .getRootView() instead of .getDecorView() in order to make it work. – Heitor Jul 26 '16 at 08:32
  • Seems this doesn't work for some Android versions (6.0.1 and even Android P) in case of an incoming call: https://issuetracker.google.com/issues/111315651 . How come? Is there a way to overcome this? – android developer Jul 11 '18 at 14:30
6

Chris Banes gist shows a nice Helper Class we can use to set the immersive mode for all Versions from HoneyComb to Lollipop https://gist.github.com/chrisbanes/73de18faffca571f7292.

Update: I tried get it from his github repo to include in my project, but i had to clone the gist files into my project and adjsut the packagename. If someone knows how to include it properly as a dependency, u r welcome to help me.

I added it in my FullScreenActivity i want to use the ImmersiveStickyMode like this:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

        final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        SystemUiHelper uiHelper =  new SystemUiHelper(this, SystemUiHelper.LEVEL_IMMERSIVE ,flags);
        uiHelper.hide();



}
swisscoder
  • 61
  • 1
  • 2
  • Could you add some details about how you used this helper class? – abarisone Apr 11 '15 at 11:45
  • 2
    @swisscoder I've create a github project which allows you to use this as a Gradle dependency: https://github.com/intrications/SystemUiHelper – Intrications Aug 09 '15 at 15:12
  • Seems it doesn't work for some Android versions (6.0.1 and even Android P) in case of an incoming call: https://issuetracker.google.com/issues/111315651 . How come? Is there a way to overcome this? – android developer Jul 11 '18 at 14:30
3

You can create global function to go into immersive mode like:

public static void enableImmersiveMode(final View decorView) {
        decorView.setSystemUiVisibility(setSystemUiVisibility());
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                    decorView.setSystemUiVisibility(setSystemUiVisibility());
                }
            }
        });
    }


public static int setSystemUiVisibility() {
        return View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
 }

Above code will also control system UI visibility change. Hope this will help you.

Rahul Parihar
  • 640
  • 7
  • 16
  • Seems this method doesn't work for some Android versions (6.0.1 and even Android P) in case of an incoming call: https://issuetracker.google.com/issues/111315651 . How come? Is there a way to overcome this? – android developer Jul 11 '18 at 14:30
2

Much nicer and credit to William J. Francis:

   public class GameActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        /* my code you dont need this
        assets=getAssets();
        sGame= new GameView(this, GAME_WIDTH, GAME_HEIGHT);
        setContentView(sGame);  
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        */


        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }

@Override         
protected void onDoSomethingOtherImportantThing(){
...
}



}
George Papatheodorou
  • 1,539
  • 19
  • 23
0

Answer has already been given but here is How to utilize Immersive mode.

In your activity:

just before setContentview().......

call the method: toggleHideyBar();

Follow documentation documentation from Developer android and copy this method in your Activity.

sud007
  • 5,824
  • 4
  • 56
  • 63
0

android:immersive="true" will hide the Bottom system bars

<application> 
 <activity
        android:name=".CarrierActivity"
        android:label="@string/app_name"
        android:excludeFromRecents="true"
        android:immersive="true"
        android:configChanges="orientation|keyboardHidden|screenSize">
        <intent-filter>
         <action android:name="com.example.SetupWiz.SUW_CARRIER"/>
         <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
    </activity>
</application>  
anand krish
  • 4,281
  • 4
  • 44
  • 47