Roadblock
Roadblock

Reputation: 2071

Toolbar scrolling under status bar

I am trying to learn CoordinatorLayout and its features. I have used it in conjunction with AppBarLayout. My xml is as follows:

<android.support.design.widget.CoordinatorLayout

android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />

    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_gravity="bottom|end"
        style="@style/FabStyle"/>

</android.support.design.widget.CoordinatorLayout>

The problem I am facing is that the toolbar on scrolling goes underneath the status bar. I want the toolbar to scroll when I scroll the Recycler View but I dont want it to go under the status bar. How do I prevent this from happening.

Also, When I try to encapsulate the CoordinatorLayout inside another layout eg: LinearLayout, the primaryDark color defined in theme does not showup in the status bar. It shows the primaryDark color when I use it as above xml. Is there a better way to do this?

Upvotes: 13

Views: 9472

Answers (3)

ilumer
ilumer

Reputation: 125

you can add root view tag CoordinatorLayout and fitsystemwindows = true again.

Upvotes: 0

Deepak Unnikrishnan
Deepak Unnikrishnan

Reputation: 186

I had the similar problem. Mine was solved.

Added android:fitsSystemWindows="true" to theAppBarLayout. Added app:layout_scrollFlags="scroll|enterAlways|snap" for Toolbar Below is the layout I had.

<android.support.design.widget.CoordinatorLayout 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=".HomeActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.design.widget.TabLayout
        android:id="@+id/id_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/id_viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email"/>

Upvotes: 8

Ryan Johnston
Ryan Johnston

Reputation: 228

I fixed this by going into the style that controls the status bar color and changed it to the color i wanted instead of transparent. Now it appears that the status bar is on top of the toolbar.

<resources>>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

`

<resources>>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>

Upvotes: 12

Related Questions