Reputation: 923
I came from objective-c and I am an Android newbie. I am using following method that intends to change tabColor for index 0. But I would like to change default grey tab when selected. Thank you.
mTabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.CYAN);
Upvotes: 3
Views: 9125
Reputation: 1
I have used this one to solve my problem:
tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
tabs.setSelectedIndicatorColors(Color.RED);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
Upvotes: 0
Reputation: 1617
Use setOnTabChangedListener(TabHost.OnTabChangeListener l) on the TabHost:
myTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
int tab = myTabHost.getCurrentTab();
myTabHost.getTabWidget().getChildAt(tab).setBackgroundColor(Color.CYAN);
}
});
Maybe there is a simpler way, i dont have use it before ;)
Upvotes: 5
Reputation: 24325
I just changed the markup of the TabHost
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@android:color/transparent">
Upvotes: 0
Reputation: 46856
Android allows for a StateList drawable xml file that is the intended way to get the effect you are after.
The idea is you make an xml file that declares a different drawable (or color if you want plain colors) for each state. Then when you are apply that statelist drawable as the background of your View, and it will handle the "magic" of switching your view image for you so that you don't have to worry about doing it manually from java code.
so your code snippet would look something like this:
myTabHost.getTabWidget().getChildAt(tab).setBackgroundResource(R.drawable.your_state_list_filename);
Here is an example of a state list file that I've used on a button. You can copy this into an xml file in your drawables folder, then modify it to use whichever states and images you want.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/darkblue1" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/darkblue1" /> <!-- focused -->
<item android:drawable="@drawable/lightblue1" /> <!-- default -->
</selector>
I think (but am not certain) that to use colors instead of drawables you'd just change "@drawable/blahblah"
to "#FF121212"
where the first two digits are alpha, and the next 6 are hex value for the color you want.
Upvotes: 2