Search code examples
androidandroid-fragmentsmaterial-designandroid-toolbarandroid-tablayout

Toobar drops shadow on TabLayout


everybody! I have a fragment in FrameLayout container within activity, which has Navigation Drawer. Fragment has TabLayout. And the problem is, as you can see, that activity's toolbar drops shadow on fragment's TabLayout. I dont want to place TabLayout in activity's AppBar layout because I won't have access to it from fragment and, moreover, I don't need that TabLayout anywhere else.

Screenshot

This is my activity_main.xml

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

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/mainNavigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

And this is app_bar_main.xml

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/loginToolbar"
            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.AppBarLayout>

    <FrameLayout
        android:id="@+id/mainFragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

This is fragment_by_day_schedule.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

 <android.support.design.widget.TabLayout
     android:id="@+id/byDayTabLayout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:tabBackground="?attr/colorPrimary"
     app:tabMode="scrollable" />

<android.support.v4.view.ViewPager
    android:id="@+id/byDayViewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

I also want the Toolbar to hide when I scroll up list in fragment within ViewPager

Do you have any ideas how can I achive that?


Solution

  • Use this. Works on my projects.

    ViewCompat.setElevation(mAppBarLayout, 0);
    

    And your second question how to hide your toolbar? Change your parent layout linearlayout to layout.

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/loginToolbar"
                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.AppBarLayout>
    
        <FrameLayout
            android:id="@+id/mainFragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.design.widget.CoordinatorLayout>
    

    After this you have to tell android when toolbar should be hide. You should tell which view can be scroll. You have add this app:layout_behavior="@string/appbar_scrolling_view_behavior" to in your ViewPager.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
    
     <android.support.design.widget.TabLayout
         android:id="@+id/byDayTabLayout"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         app:tabBackground="?attr/colorPrimary"
         app:tabMode="scrollable" />
    
    <android.support.v4.view.ViewPager
        android:id="@+id/byDayViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </LinearLayout>