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.
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>
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.
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.
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.
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.
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.
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.
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.
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
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);
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With