Reputation: 63
This code makes status bar transparent on Android 10 and before
//makes actionbar transparent activity.window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
However in android 11 it doesn't disappear
Android 11
Android 8
My styles.xml file look like this
<resources xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<style name="DropitTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<!-- color used by navigation icon and overflow icon -->
<item name="colorOnPrimary">@color/colorPrimary</item>
</style>
<style name="Toolbar_text_style" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<!-- color used by navigation icon and overflow icon -->
<item name="android:textSize">@dimen/textSizeHuge</item>
</style>
</resources>
The manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/DropitTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
and last the themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<!-- Base application theme. -->
<style name="Theme.Dropit" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryVariant">@color/colorSecondary</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/colorSecondary</item>
<item name="colorSecondaryVariant">@color/colorSecondaryLight</item>
<item name="colorOnSecondary">@color/white</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
As you can see in the Manifest I'm using the styles theme not the themes theme. So how can i fix this problem?
Upvotes: 4
Views: 4354
Reputation: 356
for notch display or camera inside the screen this code work for me. I checked on Redmi note10 :
private fun hideSystemUI() {
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_LOW_PROFILE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.attributes.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
}
call this right after super.onCreate(savedInstanceState)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
hideSystemUI()
setContentView(R.layout.activity_main)
//do you work
}
Upvotes: 1
Reputation: 131
I've found compatible sollution to set nav bar and status bar transparent.
Make sure to remove fitsSystemWindow=true in your code if you used it to achieve status bar transparency, otherwise it won't work correctly.
If you want to set only status bar transparent and not nav bar:
add
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
}
Set <item name="android:statusBarColor">@android:color/transparent</item>
in
your app theme
Add bottom inset to the main view container. In my case it's frameLayout, which displays fragments
ViewCompat.setOnApplyWindowInsetsListener(binding.main_container) {
view, windowInsets->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin = insets.bottom
}
WindowInsetsCompat.CONSUMED
}
Upvotes: 1
Reputation: 31
I had the same problem and find out this solution.
val controller = requireActivity().window.insetsController
controller?.systemBarsBehavior = BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
controller?.hide(WindowInsets.Type.statusBars())
If you call it from Activity requireActivity() is not needed. I had some problems where on some fragments it started to reset to the standard status bar, so I called it on every onCreate() and it fixed it.
Upvotes: 3