Grender
Grender

Reputation: 1619

Hide Navigation Bar, Status Bar and Toolbar on tap

I was wondering how to hide the Status Bar, Navigation Bar and Toolbar from Android when the user taps the screen, as seen in QuickPick app:

enter image description here

I have made a lot of research but I can't find or guess how to accomplish that behaviour, can someone explain a way to do it?

Upvotes: 4

Views: 5548

Answers (1)

kelvincer
kelvincer

Reputation: 6138

This is my solution:

button.setOnClickListener(new View.OnClickListener() {

        boolean show = true;

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "CLICK", Toast.LENGTH_SHORT).show();
            if (show) {
                toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
                hideSystemUI();
                show = false;
            } else {
                toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
                showSystemUI();
                show = true;
            }
        }
    });

private void hideSystemUI() {
    // Set the IMMERSIVE flag.
    // Set the content to appear under the system bars so that the content
    // doesn't resize when the system bars hide and show.
    View decorView = getWindow().getDecorView();
    decorView.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);

}

private void showSystemUI() {
    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);


}

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 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:id="@+id/rootView"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#000"
 android:fitsSystemWindows="true"
 tools:context=".MainActivity">
 .....................

Do not forget to set fitsSystemWindows to true in the main view

Upvotes: 2

Related Questions