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>
Changing the color of selected tab
you can do it like this in your tab layout widget
<android.support.design.widget.TabLayout
app:tabBackground="@drawable/selector"
/>
and define your selector.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/tab_selected_color"
android:state_selected="true"/>
<item
android:drawable="@color/tab_unselected_color"
android:state_selected="false"/>
</selector>
How to change color of tab selected in tablayout
For anyone who is looking for a solution to this. I was able to find one. You can apply the color to the layout of the child of the tabLayout. For some reference code this is what I used a worked really well for me
final LinearLayout tabsContainerLayout = (LinearLayout)expenseTabs.getChildAt(0);
Linear LayouttempLinearLayout = (LinearLayout)tabsContainerLayout.getChildAt(selectedTabPosition);
tempLinearLayout.setBackgroundColor(Color.RED);
How to set Tab background color the new TabLayout material components
Here are the steps to set the Tab background color:
first the code and then some brief explanation:
The code:
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:tabBackground="@drawable/tab_background_color_selector"
app:tabIconTint="@color/tab_content_color_selector"
app:tabIndicator="@null"
app:tabSelectedTextColor="@color/md_white_1000"
app:tabTextColor="@color/secondaryColor">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_list"
android:text="list" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_calender"
android:text="calender" />
</com.google.android.material.tabs.TabLayout>
drawable/tab_background_color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/secondaryColor" android:state_selected="true" />
<item android:drawable="@color/md_white_1000" />
</selector>
color/tab_content_color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/md_white_1000" android:state_selected="true" />
<item android:color="@color/secondaryColor" />
</selector>
The Explanation
tabBackground: is the tab background and should be a drawable selector.
tabIconTint: is for icon color and it must me color selector.
tabIndicator="@null" : as it is useless in this case.
tabSelectedTextColor, tabTextColor are self explanatory.
How to change background color of Tablayout from a fragment?
You can change background color of tablayout using addOnTabSelectedListener
of tablyout
according to position of tabs, like below code:
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
switch (tab.getPosition()){
// Change color of tab layout according to tab position
case 0:
tabLayout.setBackgroundColor(getResources().getColor(R.color.black));
break;
case 1:
tabLayout.setBackgroundColor(getResources().getColor(R.color.teal_200));
break;
default:
tabLayout.setBackgroundColor(getResources().getColor(R.color.black));
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Changing the background color of a Tab in TabLayout (Android design support library) doesn't occupy the entire tab space
Define a selector as a drawable, and also have a drawable for the selected/unselected states.
For this solution, I started with the code from this answer, and then added the functionality that changes the background color for the current Tab.
First, the selector, tab_background.xml
in the drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/tab_background_selected" android:state_selected="true" />
<item android:drawable="@drawable/tab_background_unselected" android:state_selected="false" android:state_focused="false" android:state_pressed="false" />
</selector>
Then, tab_background_selected.xml
in the drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#d13fdd1a" />
</shape>
Then, tab_background_unselected.xml
in the drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#3F51B5" />
</shape>
Finally, in your styles.xml
, specify the selector to use, and also specify the tab indicator style, since the app:tabIndicatorColor
property in the TabLayout will now be ignored:
<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
<item name="tabBackground">@drawable/tab_background</item>
<item name="tabIndicatorColor">#ff00ff</item>
<item name="tabIndicatorHeight">2dp</item>
</style>
Result with the example colors above:
Additional Note:
Tested with the 23.3.0 versions of the support library components:
dependencies {
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:cardview-v7:23.3.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
}
Android - How to change the background color or the entire TabLayout dynamically?
I solved it by checking the boolean that I have set for night theme prior to setting the content view in onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences nightModeSwitchState = getSharedPreferences("NightModeSwitchState", 0);
mNightModeSwitchState = nightModeSwitchState.getBoolean("NightModeSwitchState", false);
if (mNightModeSwitchState) {
setContentView(R.layout.activity_book_night);
} else {
setContentView(R.layout.activity_book);
}
I have two layouts made both include tabLayouts. One of them references a style file for tabLayout set to night mode colors with the dark background and the layout references a style file set to regular colors for the tablayout.
<style name="CategoryTab" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">@android:color/white</item>
<item name="tabSelectedTextColor">@android:color/white</item>
<item name="tabTextAppearance">@style/CategoryTabTextAppearance</item>
<item name="tabBackground">@drawable/tab_regular_theme</item>
<item name="android:textColorSecondary">@android:color/white</item>
</style>
<style name="CategoryTabNight" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">@android:color/white</item>
<item name="tabSelectedTextColor">@android:color/white</item>
<item name="tabTextAppearance">@style/CategoryTabTextAppearance</item>
<item name="tabBackground">@drawable/tab_layout_dark_mode</item>
<item name="android:textColorSecondary">@android:color/white</item>
</style>
The style files reference different drawables that control the background colors as you can see above.. and here are the drawables...
Here is night mode drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/dark_grey" android:state_selected="true"/>
<item android:drawable="@color/dark_grey"/>
</selector>
Here is regular mode drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/app_bar" android:state_selected="true"/>
<item android:drawable="@color/app_bar"/>
</selector>
Everything else is the layouts are identical as not to mess anything up just the style files for the tablayouts are changed. I hope this makes sense. I tested it and it works for me. I have tried so many other things and nothing worked. It is important to call Shared Preference to get the value before checking the boolean. I hope this helps someone.
How to change selected Tab Text color using TabLayout from code in Android?
If you are using the design support library add this code to your tab activity.
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FF0000"));
tabLayout.setSelectedTabIndicatorHeight((int) (5 * getResources().getDisplayMetrics().density));
tabLayout.setTabTextColors(Color.parseColor("#727272"), Color.parseColor("#ffffff"));
This will set the tab text color as well as tab indicator color in your tab activity.
Related Topics
Android Maps API V2 with Custom Markers
How to Get Current Flavor in Gradle
How to Solve Execution Failed for Task ':App:Compileflutterbuilddebug'
What Does the Layoutinflater Attachtoroot Parameter Mean
Convert Arraylist<Mycustomclass> to JSONarray
Programmatically Relaunch/Recreate an Activity
Spinner Does Not Wrap Text -- Is This an Android Bug
How to Turn Speaker On/Off Programmatically in Android 4.0
Programmatically Adding Tablerow to Tablelayout Not Working
Error:Execution Failed For Task ':App:Transformclasseswithjarmergingfordebug'
Activity Layout: Fragment Class: VS Android:Name Attributes
How Can Make My Viewpager Load Only One Page at a Time Ie Setoffscreenpagelimit(0);
Add Shadow to Custom Shape on Android
Pick Any Kind of File via an Intent in Android
How to Use Onsaveinstancestate() and Onrestoreinstancestate()