The blue line that you see at the bottom of a selected tab is actually a 9-patch drawable. A set of 6 different 9-patch drawables is used to create a state-selector drawable. This state-selector drawable is used as the background for the View that you use with TabSpec.setIndicator(View)
.
Following combinations of states is addressed in the default state-selector drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_holo" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_holo" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />
So, if you want to create a StateListDrawable, you should address all these combinations of states. Also, ColorDrawables will fill the entire tab with the specified color. If you only need to change the blue line at the bottom, you'll need to create 9-patch drawables yourself.
Now, go to:
[ android sdk installation directory on your hard drive ] > [ sdk ] > [ platforms ] > [ android-XX (XX > 11) ] > [ data ] > [ res ] > [ drawable-hdpi ]
Locate the corresponding drawables (for example, the first drawable you'll look for is tab_unselected_holo.9.png
) from the state-selector above. Open them with an image editor (http://pixlr.com/editor/ would be fine) and change the solid blue portion with the color of your choice. Save these drawables in your project's res/drawable-hdpi
folder. Be careful about naming them with .9.png
extension. Create a state-selector from the code above and change the drawables to those that you created. Save this state-selector in res/drawable
of your project. For the following code, I am assuming you named this state-selector my_tab_drawable.xml
:
Create a xml layout file named tab_indicator.xml
. We will use it for creating tab's view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/my_tab_drawable" <<==== state-selector drawable
android:gravity="center"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="@+id/tabsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dip" />
</LinearLayout>
Change your makeTabIndicator(String, Context)
to:
private View makeTabIndicator(String text, Context context) {
// Inflate the layout file we defined above
View view = LayoutInflater.from(context).inflate(R.layout.tab_indicator, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
Notice that we are returning View
in place of TextView
. You'll be using makeTabIndicator(String, Context)
as:
mtTabSpec.setIndicator(makeTabIndicator("TAB TEXT", this));
Or if you want to create the TextView dynamically(as you are doing currently), you do not need to create my_tab_drawable.xml
, or define the layout tab_indicator.xml
:
private TextView makeTabIndicator(String text, Context context) {
int tabHeight = 44;
//String tab_text_color = context.getString(R.string.fixed_tab_text_color);
TextView tabView = new TextView(getContext());
//tabView.setBackgroundColor(Utils.getColor("#0a223a"));
LayoutParams lp3 = new LayoutParams(LayoutParams.WRAP_CONTENT, CommonUtils.getDimension(tabHeight), 1);
//lp3.setMargins(1, 0, 1, 0);
tabView.setLayoutParams(lp3);
tabView.setText(text);
//tabView.setTextColor(Utils.getColor(tab_text_color));
tabView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
StateListDrawable sld = new StateListDrawable();
sld.addState(new int[] { android.R.attr.state_selected }, context.getResources().getDrawable(R.drawable.tab_selected_holo_changed));
sld.addState(new int[] { android.R.attr.state_focused }, context.getResources().getDrawable(R.drawable.tab_unselected_focused_holo_changed));
sld.addState(new int[] { android.R.attr.state_focused, android.R.attr.state_selected },
context.getResources().getDrawable(R.drawable.tab_selected_focused_holo_changed));
sld.addState(new int[] { android.R.attr.state_pressed },
context.getResources().getDrawable(R.drawable.tab_unselected_pressed_holo_changed));
sld.addState(new int[] { android.R.attr.state_selected, android.R.attr.state_pressed },
context.getResources().getDrawable(R.drawable.tab_selected_pressed_holo_changed));
sld.addState(new int[] { android.R.attr.state_focused, android.R.attr.state_pressed },
context.getResources().getDrawable(R.drawable.tab_unselected_pressed_holo_changed));
sld.addState(new int[] { android.R.attr.state_focused, android.R.attr.state_pressed, android.R.attr.state_selected },
context.getResources().getDrawable(R.drawable.tab_selected_pressed_holo_changed));
tabView.setBackgroundDrawable(sld);
// Consider increasing the padding values
tabView.setPadding(2, 0, 2, 0);
return tabView;
}
You will still need to create the 9-patch drawable.
If you need help with preparing/changing the 9-patch drawables, let me know.