How to Change the Background of an Android Tab Widget

How do I change the background of an Android tab widget?

What if you register for TabHost.OnTabChanged events and call mTabHost.getCurrentTabView() to get the View, then view.setBackgroundResource()?

How do I change a tab background color when using TabLayout?

What finally worked for me is similar to what @如果我是DJ suggested, but the tabBackground should be in the layout file and not inside the style, so it looks like:

res/layout/somefile.xml:

<android.support.design.widget.TabLayout
....
app:tabBackground="@drawable/tab_color_selector"
...
/>

and the selector
res/drawable/tab_color_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/tab_background_selected" android:state_selected="true"/>
<item android:drawable="@color/tab_background_unselected"/>
</selector>

background color of tabs widget

From https://stackoverflow.com/a/38835247/4758255 @will-molter answer:

This can actually be done using XML themes. The TabWidget uses android:textColorPrimary for the selected tab and android:textColorSecondary for the unselected ones. Thus, you can achieve a text color change like this:

In styles.xml:

<style name="TabWidgetTheme" parent="AppTheme">
<item name="android:textColorPrimary">@color/your_primary_color</item>
<item name="android:textColorSecondary">@color/your_secondary_color</item>
</style>

In your layout:

<TabHost
android:layout_height="match_parent"
android:layout_width="match_parent"
android:theme="@style/TabWidgetTheme"/>

Note that the android:theme should not be directly in the TabWidget itself, but rather the containing TabHost or similar.

How to create a customize android tab widget, that the selected tab will change its default color background?

u can change tabwidget colour

public static void setTabColor(TabHost tabhost) {
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) {
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected
}
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected

}

How to Set Background Color TabHost

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

public void onTabChanged(String arg0) {
for (int i = 0; i < tab.getTabWidget().getChildCount(); i++) {
tab.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.tab_selected); // unselected
}
tab.getTabWidget().getChildAt(tab.getCurrentTab())
.setBackgroundResource(R.drawable.tab_unselected); // selected

}
});

Try this method, I hope this will help you.

How to change TabWidget color?

put these lines in your xml-

app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/black"

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabTextAppearance="@style/MyTabLayoutTextAppearance"
app:tabIndicatorColor="@color/black"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/black"/>

How to change the background color of Tab widget in Android?

This might help you:

http://www.gregbugaj.com/?p=6

Ger



Related Topics



Leave a reply



Submit