Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tablayout update page title of custom view tabs

I made a tablayout tabs, which has custom layout for tabs, Sometimes I wish to change the title of those tabs. but I don't get right way of doing it, kindly help me.

my custom_tab.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/titttle"
        android:textStyle="bold"
        android:text="MYTITLE"
        android:paddingLeft="10dp"/>

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/count"
        android:textStyle="bold"
        android:text="1"
        android:paddingLeft="10dp"/>

</RelativeLayout> 

and in MainActivity.java

tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
 tabLayout.getTabAt(0).setCustomView(R.layout.tab_custom_view);
 tabLayout.getTabAt(1).setCustomView(R.layout.tab_custom_view);
TextView textView = (TextView)tabLayout.getChildAt(1).getRootView().findViewById(R.id.titttle);
textView.setText("New Title");

here

TextView textView = (TextView)tabLayout.getChildAt(1).getRootView().findViewById(R.id.titttle);
    textView.setText("New Title");

is not working, help me out to update the custom tab's textview.

like image 334
Vrangle Avatar asked Jul 08 '15 20:07

Vrangle


People also ask

How do you set a title in TabLayout?

The title and icon of Tabs are set through setText(int) and setIcon(int) methods of TabListener interface respectively. Tabs of layout are attached over TabLayout using the method addTab(Tab) method. We can also add tab item to TabLayout using TabItem of android design widget.

How we can add tabs at runtime in TabLayout?

You create tabs via newTab() . From there you can change the tab's label or icon via TabLayout. Tab. setText(int) and TabLayout.


1 Answers

You need to inflate the view first.

View v = View.inflate(context, R.layout.tab_custom_view, null);
Text textView = (TextView) v.findViewById(R.id.titttle);
textView.setTitle("New Title");
tabLayout.getTabAt(0).setCustomView(v);
like image 198
DEIONaLiMs Avatar answered Sep 28 '22 08:09

DEIONaLiMs