Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android R hide statusbar -> move view down and a black rectangle appears

Android R + Pixel 4 Emulator : When I hide the status bar, the main view move down and a black square (same size as status bar) appears at the top. It was working before Android 11. It look simple but I'm not able to find a solution... And I need a fullscreen mode.

I tried with the SYSTEM_UI_FLAG_FULLSCREEN (that is now deprecated) and the controller.hide(WindowInsets.Type.statusBars()); The two ways have the same issue.

enter image description here

Here is the main code: MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Button buttonShow = findViewById(R.id.b1);
    buttonShow.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
           showBars();
        }
    });

    Button buttonHide = findViewById(R.id.b2);
    buttonHide.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
          hideBars();
        }
    });

    showBars();
}

public void showBars(){

    View decorView = getWindow().getDecorView();
    int uiOptions =  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;

    decorView.setSystemUiVisibility(uiOptions);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

    // New code For Android 11, same effect...
    /*
    WindowInsetsController controller = getWindow().getInsetsController();
    if(controller != null) {
        controller.show(WindowInsets.Type.navigationBars());
        controller.show(WindowInsets.Type.statusBars());
        controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    }*/
}

public void hideBars(){


    View decorView = getWindow().getDecorView();
    int uiOptions = 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;


    decorView.setSystemUiVisibility(uiOptions);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

    // New code For Android 11, same effect...
    /* WindowInsetsController controller = getWindow().getInsetsController();
      if(controller != null) {
          controller.hide(WindowInsets.Type.navigationBars());
          controller.hide(WindowInsets.Type.statusBars());
          controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
      }*/

}

}

activity_main.xml

    <?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@color/purple_200"
    android:id="@+id/layoutMain">


    <Button
        android:id="@+id/b1"
        android:layout_marginTop="200dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show" />

    <Button
        android:id="@+id/b2"
        android:layout_marginTop="100dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hide"
/>

</RelativeLayout>
like image 296
user7590744 Avatar asked Oct 29 '20 16:10

user7590744


People also ask

What is Android Statusbar?

What Is the Android Status Bar? The status bar on Android is the bar of icons running across the top of your screen. The top right corner is dedicated to the major status of your device, while the left is used for app notifications.

How do I remove kotlin status bar?

To hide status bar in Android using Kotlin, we can request that the visibility of the status bar or other screen/window decorations be changed by setting window. decorView. systemUiVisibility with View. SYSTEM_UI_FLAG_FULLSCREEN in the Activity Kotlin file.

What hide status bar means?

This lesson describes how to hide the status bar on different versions of Android. Hiding the status bar (and optionally, the navigation bar) lets the content use more of the display space, thereby providing a more immersive user experience.

How do I hide the status bar on Android 4?

Hide the Status Bar on Android 4.0 and Lower. You can hide the status bar on Android 4.0 (API level 14) and lower by setting WindowManager flags. You can do this programmatically or by setting an activity theme in your app's manifest file.

Why hide the status bar in the navigation bar?

Hiding the status bar (and optionally, the navigation bar) lets the content use more of the display space, thereby providing a more immersive user experience. Figure 1. Visible status bar.

How to hide the status bar when the app is running fullscreen?

Use theme "Theme.NoTitleBar.Fullscreen" and try setting "android:windowSoftInputMode=adjustResize" for the activity in AndroidManifest.xml. You can find details here. With the above approach, my status bar can be hidden when the app is running full screen, however, I can still see the status bar while I swiping down the page.

Can I show the action bar without the status bar?

Figure 2 shows an app with a hidden status bar. Note that the action bar is hidden too. You should never show the action bar without the status bar. Figure 2. Hidden status bar.


3 Answers

This worked for me:
Create a style resource file with v27 configuration for the activities you want in fullscreen and without cutout:

<style name="Theme.MyApp.ActivityFullscreenNoCutout">
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>

And assign the style to your activity in the manifest:

<activity android:name=".MyActivity"
        android:theme="@style/Theme.MyApp.ActivityFullscreenNoCutout"/>

Link: https://developer.android.com/guide/topics/display-cutout

If you have an action/app bar, you'll have to add the toolbar to your activity and not use the default one.

Link: https://developer.android.com/training/appbar/setting-up

like image 183
Olivier M Avatar answered Dec 06 '22 11:12

Olivier M


Try this code snippet.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
    }
like image 20
Eyosiyas Avatar answered Dec 06 '22 10:12

Eyosiyas


Somewhere in activity code...

    @TargetApi(Build.VERSION_CODES.R) 
void hideStatusBar_with_API30() {
    View decorView = getWindow().getDecorView();
    // Order swiped status bar to appear semitransparent.
    // If you do not that
    //  then once appeared bar will not disappear after nor few seconds nor you gesture.
    decorView.getWindowInsetsController().setSystemBarsBehavior(
            WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    // Hide the status bar.
    decorView.getWindowInsetsController().hide(WindowInsets.Type.statusBars());
    // The status bar is allowed to appear with a swipe gesture.
    // When it is then app layout is resized.
    // To prevent changing the layout, you need to add:
    getWindow().setDecorFitsSystemWindows(false);
    // The status bar will appear over stable layout
}
like image 45
Casual Passerby Avatar answered Dec 06 '22 12:12

Casual Passerby