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.
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.
You create tabs via newTab() . From there you can change the tab's label or icon via TabLayout. Tab. setText(int) and TabLayout.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With