Search code examples
androidandroid-actionbar

Gap between Actionbar and statusbar, non translucent


I got a gap between the statusbar and actionbar and just can't get it to go away. I have played with android:fitsSystemWindows and below you see to examples of it. I am using supportActionBar by using AppCompatActivity. It is setup with Navigation components:

val navController = navController()
val appBarConfiguration = AppBarConfiguration(setOf(R.id.navigation_onboarding, R.id.projectsFragment, R.id.navigation_menu))
setupActionBarWithNavController(navController, appBarConfiguration)

The fragment shown is just plain with a Constaintlayout without any android:fitsSystemWindows attributes.

Any clue to how to get rid of this gap?

Screenshot 1: - - - - - - - - - - - - - - - - - - - - - - - Screenshot 2:

Main activity layout:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:fitsSystemWindows="true" <!--Screenshot 1-->
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/navigationHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fitsSystemWindows="true" <!--Screenshot 2-->
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navigation" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        android:visibility="gone"
        app:itemIconTint="@color/bottom_navigation"
        app:labelVisibilityMode="unlabeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

Theme:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    ...
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">false</item>
</style>

Solution

  • We ended up solving it by adding a custom Toolbar and have translucent status bar enabled:

    toolbar.adjustHeightUnderStatusBar(this)
    setSupportActionBar(toolbar)
    

    With this view layout

    <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/container"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/grey"
            android:elevation="4dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:title=""
            app:titleTextColor="@color/darkBlue"
            tools:ignore="HardcodedText" />
    
        <fragment
            android:id="@+id/navigationHostFragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/toolbar"
            app:navGraph="@navigation/navigation" />
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:background="?android:attr/windowBackground"
            android:visibility="gone"
            app:itemIconTint="@color/bottom_navigation"
            app:labelVisibilityMode="unlabeled"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav_menu" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    adjustHeightUnderStatusBar function:

    fun Toolbar.adjustHeightUnderStatusBar(activity: BaseActivity) {
        val statusBarHeight = 24.px
        var actionBarHeight = 48.px
        val value = TypedValue()
        if (activity.theme.resolveAttribute(android.R.attr.actionBarSize, value, true)) {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(value.data, resources.displayMetrics)
        }
    
        val layoutParams = layoutParams as ConstraintLayout.LayoutParams
        layoutParams.height = actionBarHeight + statusBarHeight
        this.layoutParams = layoutParams
        setTopPadding(statusBarHeight)
    }