# How to apply text to my tabs in actionbar?



## sherlockjones (Apr 4, 2013)

Mod Type:: Application

Difficulty:: Moderate

Mod Base:: Other

Mod Status:: Beta

Apply In:: Fastboot

Requires Root:: No

Source:: 
I have this code where I get 5 Tabs that say Tab 1 - Tab5 but I want to put my own text in , here is my code:

getSupportActionBar().setNavigationMode(ActionBar. NAVIGATION_MODE_TABS);
for (int i = 1; i <= 5; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText("Tab " + i);
tab.setTabListener(this);
getSupportActionBar().addTab(tab);
}
}


----------



## yarly (Jun 22, 2011)

App forum is for developers to post their apps only please. Moved to the development forum


----------



## JBirdVegas (Jun 11, 2011)

You are very close. Just check your int's position then respond correctly

ie
(using single custom text)

```
<br />
getSupportActionBar().setNavigationMode(ActionBar. NAVIGATION_MODE_TABS);<br />
String customText = "Some Text";<br />
for (int i = 1; i <= 5; i++) {<br />
	ActionBar.Tab tab = getSupportActionBar().newTab();<br />
	if (i == 5) {<br />
		tab.setText(customText);<br />
	} else {<br />
		tab.setText("Tab " + i);<br />
	}<br />
	tab.setTabListener(this);<br />
	getSupportActionBar().addTab(tab);<br />
}
```
or...

(use all custom texts)

```
<br />
getSupportActionBar().setNavigationMode(ActionBar. NAVIGATION_MODE_TABS);<br />
String[] tabsText = { "Tab 1", "Tab 2", "Tab 3", "Tab 4", "someCustomText" };<br />
// you must start with 0 in this case because the indexing of Arrays<br />
// begins with a value in position 0;<br />
for (int i = 0; i < 5; i++) {<br />
	ActionBar.Tab tab = getSupportActionBar().newTab();<br />
	tab.setText(tabsText[i]);<br />
	tab.setTabListener(this);<br />
	getSupportActionBar().addTab(tab);<br />
}
```
It really just depends on how custom you want your text to be... I'm guessing Tab 1... Tab 4 probably is not the production code so I would use the latter of the two examples


----------



## yarly (Jun 22, 2011)

This is bit more elegant:

In /res/values/arrays.xml :


```
<br />
<?xml version="1.0" encoding="utf-8"?><br />
<resources><br />
<br />
<string-array name="listTabs"><br />
<item>Tab 1</item><br />
<item>Tab 2</item><br />
</string-array><br />
</resources><br />
```
In your activity (make sure to implement ActionBar.TabListener or you're going to have a bad time):


```
<br />
ActionBar actionBar = getActionBar();<br />
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);<br />
String[] tabTitles = getResources().getStringArray(R.array.listTabs);<br />
<br />
for (int i = 0; i < tabTitles.length; ++i) {<br />
actionBar.addTab(actionBar.newTab()<br />
.setText(tabTitles[i])<br />
.setTabListener(this).setTag(i));<br />
}<br />
```


----------



## JBirdVegas (Jun 11, 2011)

I agreed Yarly's version is more eloquent but if we are going that far lets allow localization; Just link the array to a string resource something like

in res/values/arrays.xml

```
<br />
<string-array name="listTabs"><br />
<item>@string/tab_one</item><br />
<item>@string/tab_two</item><br />
</string-array><br />
</resources>
```
in res/values/strings.xml

```
<br />
<string name="tab_one">Tab 1</string><br />
<string name="tab_two">Tab 2</string><br />
```


----------

