12

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>
user7590744
  • 167
  • 1
  • 6
  • you have to apply these code https://stackoverflow.com/questions/43511326/android-making-activity-full-screen-with-status-bar-on-top-of-it – axar Mar 20 '21 at 06:41

3 Answers3

4

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

Olivier M
  • 41
  • 3
  • 3
    Thank you so much. I put it directly in to theme and worked fine too: shortEdges – arun Oct 21 '21 at 22:03
  • Thanks Arun! This is how it worked for me: shortEdges – Sabri Meviş Aug 12 '23 at 10:26
  • It worked directly in theme for me too like @arun - how do you set v27 configuration though? Is that increasing minSdkVersion to 27? Is this required? I was hoping to target 24+ – Noitidart Aug 30 '23 at 12:56
  • It is not required and doesn't increase the minSdkVersion. Creating a separate resource file lets you separate the code between the different configurations you want. The configuration in your theme file will just be ignored for api 26 and below. – Olivier M Aug 31 '23 at 15:50
1

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);
    }
Eyosiyas
  • 1,422
  • 1
  • 9
  • 25
1

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
}