Reputation: 9225
I have the following ActionBar tab in my app. I was wondering what is the best way to change the colors around to match my app.
I saw the following code:
ActionBar ab = getActionBar();
//ab.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
But that line changes color for all the tabs to just one color.
The Tab code in my app is:
ActionBar ab = getActionBar();
//ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff"))); not changing the tab color
//ab.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
ab.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );
Tab tab = ab.newTab()
.setText( "TY1" )
.setTabListener(
new MyTabListener( this, TY1.class.getName() ) );
ab.addTab( tab );
tab = ab.newTab()
.setText( "TY2" )
.setTabListener(
new MyTabListener( this, TY2.class.getName() ) );
ab.addTab( tab );
tab = ab.newTab()
.setText( "ty3" )
.setTabListener(
new MyTabListener( this, TY3.class.getName() ) );
ab.addTab( tab );
Any and all help is appreciated. I can use XML as well, if someone points me in the right direction.
Upvotes: 0
Views: 8303
Reputation: 1246
For those looking for more detailed example code on how to create a custom tab view and then change the tab's formatting, you can see my answer at https://stackoverflow.com/a/28389366/3268329. That question had to do with changing the tab text color when a particular tab is selected, but you can easily modify the code to change the background instead or do whatever else you need.
https://stackoverflow.com/a/18491600/3268329 (S.Thiongane's answer) also shows how to set up different background resources.
Upvotes: 1
Reputation: 38595
You can set a custom View for each tab. Create a new layout resource for the tab (it can just be a TextView). Leave its background empty and make a nine-patch drawable for the selection indicator. Get a LayoutInflater
using
LayoutInflater inflater = getSystemService(LAYOUT_INFLATER_SERVICE);
Then for each tab, you can do this:
Tab tab = ab.newTab()
.setText("TY1")
.setTabListener(new MyTabListener(this, TY1.class.getName()));
View tabView = inflater.inflate(R.layout.my_tab_layout, null);
tabView.setBackgroundColor(...); // set custom color
tab.setCustomView(tabView);
ab.addTab(tab);
Upvotes: 2
Reputation: 2823
You can change ActionBar's background color by defining custom style, as:
<resources>
<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">ANY_HEX_COLOR_CODE</item>
</style>
</resources>
and create your action bar then download it. After you download it , you can use it in your project.
Upvotes: 1