Search code examples
androidandroid-layoutandroid-viewpagerandroid-actionbarandroid-relativelayout

ViewPager under ActionBar in RelativeLayout


ViewPager is below ActionBar. How can I make it under it?

layout:

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbarlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <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/AppTheme.PopupOverlay" />

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

    <android.support.design.widget.BottomNavigationView

        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@drawable/selector"
        android:background="@color/colorPrimary"
        app:itemTextColor="@drawable/selector"
        app:menu="@menu/activity_homebottomnav" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation"
        android:layout_below="@id/appbarlayout"
        android:layout_alignParentTop="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</RelativeLayout>

It's on top of BottomNavigationView as it should be, but is under ActionBar.

app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_below="@id/appbarlayout"

Doesn't work. And I don't want to use android:layout_marginTop="?android:attr/actionBarSize"

Thanks.


Solution

  • Put the AppBarLayout and the ViewPager under a android.support.design.widget.CoordinatorLayout and it should work as the behavior you're specifying only works under a CoordinatorLayout

    <RelativeLayout 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">
    
        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/bottom_navigation">
    
            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbarlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay"
                app:elevation="0dp">
    
                <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/AppTheme.PopupOverlay"
                    />
    
            </android.support.design.widget.AppBarLayout>
    
            <android.support.v4.view.ViewPager
                android: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.CoordinatorLayout>
    
    
    
        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:itemBackground="@color/colorPrimary"
            app:itemIconTint="@drawable/selector"
            android:background="@color/colorPrimary"
            app:itemTextColor="@drawable/selector"
            app:menu="@menu/tabs"
            />
    </RelativeLayout>