Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add tabs to TabLayout in layout XML file in Android?

Tags:

android

tabs

I have been studying Android for two weeks. Now I am developing my first Android project.

In my project I need to work with Tabs together with action bar. I know that is very common in Android. For action bar I am using toolbar with v7. I seriously know about action bar. But I have an issue with tab layout. The problem is I want to add tabs to tab layout in layout XML file.

For example:

<TabLayout>
//tab buttton goes here
 <TabButton1></TabButton1>
 <TabButton1></TabButton1>
 .
 .
<TabLayout>

I search so many tutorials about tablayout. But all tutorials define tabs for tab layout programmatically. How can I define tabs for tab layout in XML and define event for them programmatically by findViewById? I don't want to use button view instead of tabs.

like image 464
Wai Yan Hein Avatar asked Feb 05 '16 20:02

Wai Yan Hein


1 Answers

This should be what you want:

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            android:background="?attr/colorPrimary"
            app:tabMode="fixed">

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Popular" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Most Rated" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Favorites" />
        </android.support.design.widget.TabLayout>
like image 57
Putra Ardiansyah Avatar answered Oct 19 '22 23:10

Putra Ardiansyah