4

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?

Grender
  • 1,589
  • 2
  • 17
  • 44
  • 1
    this might help... http://developer.android.com/intl/es/training/system-ui/immersive.html – kimchibooty May 03 '16 at 22:20
  • Thanks, I can hide both `Status bar` and `Navigation bar` but not the `Toolbar`. Also, I can't make it work if I'm using https://github.com/chrisbanes/PhotoView because the image which fills the entire screen also has the `onClick()` detection of that library in order to be zoomable. – Grender May 03 '16 at 22:44
  • for the toolbar, you could call getActionBar().hide() in the activity that the view lives in. sorry if I couldn't help! :/ good luck! – kimchibooty May 03 '16 at 22:48
  • @Grender hey, did you finally figure out a clean way to do it? – Suleyman Apr 01 '18 at 16:12
  • @LieForBananas Have you figured out a solution to this gap? – Jack Nov 09 '18 at 09:44
  • @Jack not yet, I've left the problem for now, I managed to hide everything, but I still have the gap. I don't remember the problem exactly, but I think a temporary solution would be to make the background black. – Suleyman Nov 09 '18 at 13:07
  • @LieForBananas I finally managed to solve it, the problem was that setFitsSystemWindows(true) adds paddings (Top -> statusbar / Bottom -> Navigationbar), and for sorry when you use setFitsSystemWindows(false) it doesn't remove the paddings it added, you have to remove the paddings yourself programatically. setPadding(0, 0, 0, 0). I hope it will solve your problem as it solved mine. – Jack Nov 09 '18 at 13:16
  • @Jack oh ok, I didn't know that! Thanks a lot, I'll try that! – Suleyman Nov 10 '18 at 16:46

1 Answers1

2

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

kelvincer
  • 5,929
  • 6
  • 27
  • 32
  • which button did you clicked – Peter Walter Aug 25 '18 at 14:29
  • Any button you want. This is only a demonstration – kelvincer Aug 25 '18 at 23:01
  • @kelvincer Great answer, but here's the problem, when the navigation bar gets hidden, it leaves a blank space instead of it, my activity doesn't re-layout and extend to fill the screen. P.S: I need to keep fitsSystemWindows to be true. I also tried remving View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION to force resize but in vein. – Jack Nov 08 '18 at 05:57
  • I also tried calling invalidate & requestLayout() on the root view but I still have the blank pace. – Jack Nov 08 '18 at 05:59
  • EDIT: The root layout (RelativeLayout) extends to fill the screen. But it's child view which is set layout_alignParentBottom="true" doesn't still in its place and doesn't move down to its parent bottom which causes the gap. How to fix this? – Jack Nov 08 '18 at 06:06
  • @Jack did you fixed that? I also have same problem. The navigation and status bar's text is invisible but it is not removing from its place. – Dibas Dauliya May 06 '20 at 07:47