'status bar becoming black and icon visible on swiping in Android api 30

I am using fragment and in that fragment i have ViewPager and in ViewPager i have added two fragments.when i open the app Everything works fine but my status bar becomes black and to see the icon i need to scroll down and this is happening in android 11,below android 11 Eveything working fine.now please let me know how to fix it...please don't ignore this and answer should be in Kotlin if Possible

I am attaching the code for it also

Main Activity:

class dashact : AppCompatActivity() {

 lateinit var toolbar: Toolbar

 lateinit var coordinator: CoordinatorLayout

 lateinit var navigationdrawer: NavigationView

 lateinit var drawerLayout: DrawerLayout

var nameofuser :String?="jdghjg"

 override fun onCreate(savedInstanceState: Bundle?) {

 super.onCreate(savedInstanceState)

 setContentView(R.layout.dashact)

 toolbar = findViewById(R.id.toolbar)

 coordinator = findViewById(R.id.coordinator)

 navigationdrawer = findViewById(R.id.navigationview)

 drawerLayout = findViewById(R.id.drawerlayout)

 actionbar()

 var toggle = ActionBarDrawerToggle(

 this@dashact,

 drawerLayout,

 R.string.open,

 R.string.close

 )

 toggle.syncState()

 drawerLayout.addDrawerListener(toggle)

 hideSystemBars()

 navigtiontitle("home")

 supportFragmentManager.beginTransaction()

 .replace(R.id.frame, frag1())

 .commit()

 drawerLayout.closeDrawers()

 navigationdrawer.setNavigationItemSelectedListener {

 if (it.isChecked==true){

 it.isCheckable=true

 }else{

 it.isCheckable=false

 }

 when (it.itemId) {

 R.id.home -> {

 navigtiontitle("home")

 supportFragmentManager.beginTransaction()

 .replace(R.id.frame, frag1())

 .commit()

 drawerLayout.closeDrawers()

 }

 R.id.fav ->{

 navigtiontitle("Favourites")

 supportFragmentManager.beginTransaction()

 .replace(R.id.frame,Viewpager2())

 .commit()

 drawerLayout.closeDrawers()

 }

 R.id.privacy ->{

 navigtiontitle("Privacy Policies")

 supportFragmentManager.beginTransaction()

 .replace(R.id.frame,privacy())

 .commit()

 drawerLayout.closeDrawers()

 }

 R.id.details ->{

 navigtiontitle("My Details")

 supportFragmentManager.beginTransaction()

 .replace(R.id.frame,MyFrag())

 .commit()

 drawerLayout.closeDrawers()

 }

 }

 return@setNavigationItemSelectedListener true

 }

 }

 fun navigtiontitle(title: String) {

 supportActionBar?.title = title

 }

 fun actionbar() {

 super.setSupportActionBar(toolbar)

 supportActionBar?.title = "Bookhub"

 supportActionBar?.setHomeButtonEnabled(true)

 supportActionBar?.setDisplayHomeAsUpEnabled(true)

 }

 override fun onOptionsItemSelected(item: MenuItem): Boolean {

 when (item.itemId) {

 android.R.id.home -> drawerLayout.openDrawer(GravityCompat.START)

 }

 return super.onOptionsItemSelected(item)

 }

 fun hideSystemBars() {

 val windowInsetsController =

 ViewCompat.getWindowInsetsController(window.decorView) ?: return

 // Configure the behavior of the hidden system bars

 windowInsetsController.systemBarsBehavior =

 WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE

 // Hide both the status bar and the navigation bar

 windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())

 }

 }

xml of main activity:

<?xml version="1.0" encoding="utf-8"?>

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:id="@+id/drawerlayout"

 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"

 >

 <androidx.coordinatorlayout.widget.CoordinatorLayout

 android:id="@+id/coordinator"

 android:layout_width="match_parent"

 android:layout_height="match_parent">

 <com.google.android.material.appbar.AppBarLayout

 android:layout_width="match_parent"

 android:layout_height="wrap_content"

 android:theme="@style/ThemeOverlay.AppCompat.Dark"

 android:elevation="0dp" >

 <androidx.appcompat.widget.Toolbar

 android:id="@+id/toolbar"

 android:layout_width="match_parent"

 android:layout_height="wrap_content"

 android:minHeight="?attr/actionBarSize"

 />

 </com.google.android.material.appbar.AppBarLayout>

 <FrameLayout

 android:id="@+id/frame"

 android:layout_width="match_parent"

 android:layout_height="wrap_content"

 app:layout_behavior="@string/appbar_scrolling_view_behavior" />

 </androidx.coordinatorlayout.widget.CoordinatorLayout>

 <com.google.android.material.navigation.NavigationView

 android:id="@+id/navigationview"

 android:layout_width="wrap_content"

 android:layout_height="match_parent"

 app:headerLayout="@layout/headers"

 app:menu="@menu/icons"

 android:layout_gravity="start"/>

</androidx.drawerlayout.widget.DrawerLayout>

themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">

 <!-- Base application theme. -->

 <style name="Test" parent="Theme.MaterialComponents.DayNight.NoActionBar">

 <!-- Primary brand color. -->

 <item name="colorPrimary">@color/purple_200</item> //for action bar

 <item name="colorPrimaryVariant">@color/white</item> //can use to set status bar

 <item name="colorOnPrimary">@color/purple_200</item>

 <!-- Secondary brand color. -->

 <item name="colorSecondary">@color/teal_200</item>

 <item name="colorSecondaryVariant">@color/teal_700</item>

 <item name="colorOnSecondary">@color/black</item>

 <!-- Status bar color. -->

 <item name="android:statusBarColor" tools:targetApi="l">@color/purple_200</item>

 <!-- Customize your theme here. -->

 </style>

 <style name="splashs" parent="Theme.MaterialComponents.DayNight.NoActionBar">

 <item name="android:windowBackground">@drawable/splash</item>

 <item name="android:statusBarColor" tools:targetApi="l">@color/purple_200</item>

 </style>

</resources>


Solution 1:[1]

You are hiding your system bars(status and navigation) in hideSystemBars() method. You are getting a black status bar probably due to a display cutout. Find more about supporting display cutout here

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 Fedric Antony