Search code examples
androidandroid-tablayout

Fragment's content above Tab's text in TabLayout


I'm using TabLayout and I attach two tabs.

    tabLayout.addTab(tabLayout.newTab().setText("Featured"));
    tabLayout.addTab(tabLayout.newTab().setText("Filter"));

Then onTabSelected() I display the fragment I want according to which tab user selected.

The layout of my Activity is :

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


    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>

    <FrameLayout 
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

and the layout of the fragments is just a TextView in a LinearLayout :

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView 
        android:text="Projects List Fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

But the result I get is the following :

enter image description here

as you can see the content of the fragment overlaps Tab's text when it should appear underneath. Thanks


Solution

  • Finally, I found alternative solution for this problem.

    You can give top padding on your each Tab fragment's layout.

    Eg,

    android:paddingTop="?attr/actionBarSize"

    For instance, my first tab xml looks like below:

    <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="wrap_content"
        android:paddingTop="?attr/actionBarSize"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.upgautam.uddhav.productassignment.uicontrollers.ProductListFragment">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/product_list_string" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    Now, the screen looks like below: enter image description here