Search code examples
androidviewmenuitemandroid-tablayout

Programmatically set TabLayout items titles


enter image description here

I need to make this type of views so I thought a TabLayout with fragments would help.

But the problem is, instead of having each menu item hardcoded, I need to make the tab titles programmatically as name may change or the amount of itemmenu also change.

I think an ArrayList of Strings can help storing each item menu title.

How can I have the titles set programmatically?


Solution

  • you can do it like this example:

    TabLayout tabLayout = ...;
     /*tabItems is an Arraylist<String> with the tab names*/
     for( String tabItem: tabitems){
       tabLayout.addTab(tabLayout.newTab().setText(tabItem));
     }
    

    this is the official doc https://developer.android.com/reference/android/support/design/widget/TabLayout