mahdi pishguy
mahdi pishguy

Reputation: 1034

Android TabLayout not visible on my application

I'm trying to add simple TabLayout on my application, but it doesn't visible, view pager work fine

this layout is toolbar_layout

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:titleTextColor="#ffffff"
        app:layout_scrollFlags="scroll|enterAlways">

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

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

and my main layout is:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/content_background">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <include layout="@layout/application_toolbar"/>

            <android.support.v4.view.ViewPager
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="-25dp"
                android:layout_marginRight="-25dp"
                />
        </LinearLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigationView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/header"
            app:menu="@menu/drawer"/>

        <com.lapism.searchview.SearchView
            android:id="@+id/searchView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"/>
    </android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>

as i said view pager work fine, but tabs not visible on my application

Upvotes: 0

Views: 1198

Answers (5)

dzikovskyy
dzikovskyy

Reputation: 5087

That's because you put your TabLayout inside the Toolbar.
Try to change your toolbar_layout to this:

<android.support.design.widget.AppBarLayout>    
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:titleTextColor="#ffffff"
        app:layout_scrollFlags="scroll|enterAlways">

    </android.support.v7.widget.Toolbar>

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

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

Upvotes: 1

Ajay Sivan
Ajay Sivan

Reputation: 2989

Main Layout

        <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/content_background">

        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <include layout="@layout/application_toolbar"/>

                <android.support.v4.view.ViewPager
                    android:id="@+id/container"
                    android:layout_width="match_parent"
<!-- Change this -->
                    android:layout_height="0dp"
                    android:layout_weight="1"
<!-- Code -->
                    android:layout_marginLeft="-25dp"
                    android:layout_marginRight="-25dp"
                    />
            </LinearLayout>

            <android.support.design.widget.NavigationView
                android:id="@+id/navigationView"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                app:headerLayout="@layout/header"
                app:menu="@menu/drawer"/>

            <com.lapism.searchview.SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="gone"/>
        </android.support.v4.widget.DrawerLayout>
    </android.support.design.widget.CoordinatorLayout>

The android:layout_height of the LinearLayout is set to match parent that hides the TabLayout. Change android:layout_height to 0dp and set android:layout_weight to 1. I think this may solve your problem.

Upvotes: 1

Miki P
Miki P

Reputation: 652

Try this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/fragment_container"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             xmlns:tools="http://schemas.android.com/tools"
             tools:context=".MainActivity">
    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/action_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
        <!-- Tabs-->
        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            app:tabMode="fixed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:tabTextColor="#d3d3d3"
            android:minHeight="?attr/actionBarSize"
            />
    </android.support.design.widget.AppBarLayout>

</FrameLayout>

The FrameLayout is the main content view of the activity.

Upvotes: 0

nshmura
nshmura

Reputation: 6000

TabLayout should not be in Toolbar.

Check this repository as sample of using CoordinatorLayout, DrawerLayout, Toolbar, TabLayout.

Here is activity_main.xml and include_list_view.xml.

The view hierarchy is like this:

DrawerLayout
 - CoordinatorLayout
   - AppBarLayout
     - Toolbar
     - TabLayout
   - ViewPager
 - NavigationView

Upvotes: 1

Manic Lalo
Manic Lalo

Reputation: 1

I actually found someone else's answer here.

Tabs of TabLayout not showing

tabLayout.post(new Runnable() {
    @Override
    public void run() {
        tabLayout.setupWithViewPager(viewPager);
    }
});

Hope this helps

Upvotes: -2

Related Questions