'How to hide status bar and make content appear behind on every device?
Goal
I'm trying to hide status bar and make content appear behind it.
What I have tried
On the phone I'm currently developing (Samsung Galaxy S9+), I managed to make it work with this function
public static void removeAndroidNavBar(View view){
int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
view.setSystemUiVisibility(uiOptions);
}
which is called on the OnCreate() method after SetContentView() like this
removeAndroidNavBar(getWindow().getDecorView());
Problem
HOWEVER, I run the code on a OnePlus Nord 2 which is on OxygenOs, and this doesn't work. The status bar is not displayed (there is a black space instead), but I can't figure out how to make it disappear completely, and make content appear behind it.
The doc
As the documentation says HERE,
On Android 4.1 and higher, you can set your application's content to appear behind the status bar, so that the content doesn't resize as the status bar hides and shows. To do this, use SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN. You may also need to use SYSTEM_UI_FLAG_LAYOUT_STABLE to help your app maintain a stable layout.
which is what I'm doing, and I'm encountering the problem only on the One Plus Nord 2 device
What am I doing wrong?
Solution 1:[1]
If you wants to achieve some thing like this 
try like this:-
fun Activity.makeStatusBarTransparent() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.apply {
clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
// addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(hasImmersive(context)){
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
}else {
// decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
// View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
// View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
// View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
if (Build.VERSION.SDK_INT >= 21) {
var window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// window.setStatusBarColor(context.getResources().getColor(color));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
var decor = getWindow().getDecorView();
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
/* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val controller = getWindow().insetsController
controller?.systemBarsBehavior = BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
controller?.hide(WindowInsets.Type.statusBars())
}*/
}
} else {
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
statusBarColor = Color.TRANSPARENT
}
}
Log.d("Has onscreen Navigation"," "+hasImmersive(context))
} catch (e: Exception) {
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Aris_choice |
