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 selectorres/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
Set Font for All Textviews in Activity
Accessing Files from Genymotion Sd Card
Display Data After Every 10 Seconds in Android
Get the Distance Between Two Locations in Android
How to Save the Image to Sd Card on Button Click Android
How to Handle Multiple Application Classes in Android
Fragment Lifecycle: When "Ondestroy" and "Ondestroyview" Are Not Called
Java.Lang.Numberformatexception: Invalid Int: "" in Android
How to Connect with Remote Debugger
How to Change the Background of an Android Tab Widget
Identifying Rtl Language in Android
Render Error in Android Studio 3.0 Layout Editor
Execute Task Before Android Gradle Build
Android Xml VS Java Layouts Performance