Friday, January 16, 2015

Android Managing System UI - Hiding the Navigation Bar

In Android system bars are screen areas dedicated to the display of notifications, communication of device status, and device navigation. These are displayed concurrently with the application. Apps that display immersive content, such as images or movies can temporarily dim the system bar icons for a less distractng experience, or temporarily hide the bars for a fully immersive experience. 

Hiding the Navigation Bar
Navigation bar was introduced in Android System 4.0. Normally, it is better to hide the status bar and the navigation bar together. 

Below code can hide the navigation bar. 

super.onCreate(savedInstanceState);
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
        setContentView(R.layout.activity_main);

Few notes:

- In this approach, touching anywhere on the screen will cause the navigation bar and the status bar to reappear again.  i.e. User interaction makes the elements re-appear again and the already set flags will be cleared. 

- Application can listen to the UI Visibility state changes to listen to the ui activity events and then can decide to make it reappear again, 

- Application needs to call the flags at specific points in the application, OnCreate is one place, onResume is another, onWindowFocusChanged should also be overriden to listen to the system event and then hide the system ui components again 

- The methods have effect only if the calls is from a View that is visible

- Navigating away from the view causes the flag set with the setSystemUiVisibility to be cleared. 

with the below few lines of code, application is able to show a full screen Activity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
        setContentView(R.layout.activity_main);
        ActionBar ab = getActionBar();
        if(ab != null) {
            ab.hide();
        }
        android.support.v7.app.ActionBar sab = getSupportActionBar();
        if(sab != null) {
            sab.hide();
        }
    }


References:


No comments:

Post a Comment