I have an Appbar, Tablayout and a ViewPager inside the CoordinatorLayout. But the ViewPager always overlapping every other views in the screen unless i specify a definite height other than 'MatchParent' or 'WrapContent'. i tried several things but nothing worked for me other than setting the height dynamically according to the screen size. I wonder! there must be some way to do this in the xml itself.
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/logocolor"
android:id="@+id/appbar"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar1"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:title="FlashBook"
app:titleTextColor="@color/whitetish"
app:layout_scrollFlags="scroll|enterAlways|snap"
/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentStart="true"
android:background="@color/logocolor"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom"
android:layout_gravity="bottom">
</android.support.design.widget.TabLayout>
<include
layout="@layout/viewpager_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_anchor="@id/tablayout"
app:layout_anchorGravity="bottom"
android:layout_gravity="bottom"
>
</include>
</android.support.design.widget.CoordinatorLayout>
This is my activityLayout and..
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/viewpagermain"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="@layout/activity_appbartestactvity"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager>
this is the viewpager_layout..
Try adding the ViewPager
directly as the CoordinatorLayout
child:
<android.support.v4.view.ViewPager
android:id="@+id/viewpagermain"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
Or if you need the <include
tag, add this line to the <include
too:
app:layout_behavior="@string/appbar_scrolling_view_behavior"
Also, it's better to use TabLayout
inside the AppBarLayout
and under the Toolbar
.
For being scrollable, add: app:layout_scrollFlags="scroll|enterAlways"
To stick there after collapsing, add: app:layout_collapseMode="pin"