Tablayout and Toolbar are not on the same height

607 views Asked by At

i am building an application in which i want the Activity to have a bottomBar and each item of it to be a fragment. The first fragment should have a viewpager. Everything works fine except the fact that TabLayout and Toolbar do not have the same height.

The problem is:

enter image description here

and my activity layout is:

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_top" />
    </android.support.design.widget.AppBarLayout>

my toolbarTop layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
    android:id="@+id/topBar"
    android:background="@color/colorPrimary">
</android.support.v7.widget.Toolbar>

and my tablayout fragment:

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

    android:layout_marginBottom="?android:attr/actionBarSize"
    android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        app:tabMaxWidth="0dp"
        app:tabGravity="fill"
        app:tabMode="fixed"
        android:elevation="6dp"
        android:layout_height="?android:attr/actionBarSize"
        android:background="@color/colorPrimary" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        android:layout_height="wrap_content" />

</LinearLayout>

I have tried this answer but it was not successful. I cannot put the tablayout in the Appbarlayout(which solves the problem) because every fragment will have a tablayout.

Any other ideas to solve this problem?

1

There are 1 answers

6
Lalit Dhameliya On

You need to include it in the AppbarLayout so, to do that Add the TabLayout to toolbarTop.xml file below the toolbar.