Tuesday, 16 March 2021

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>


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

No comments:

Post a Comment