Changing the Background Color of a Tab in Tablayout (Android Design Support Library) Doesn't Occupy the Entire Tab Space

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:

Sample Image

Sample Image

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'
}

Tab layout tab does not change background color

Instead of customView() use getCsutomView(). Code should be like:

tabLayout.getTabAt(2)?.getCustomView?.setBackgroundColor(Color.parseColor("#000000"))

This should fix your problem.

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.

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.

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 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>

change Custom Tab layout Textview background color

There is small Mistack by me.The Right is Following.

mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
@Override
public void onTabSelected(TabLayout.Tab tab)
{
if(tab.getPosition()==0)
{
tabText_customtab1.setBackgroundResource(R.color.selected_tab_color);
tabText_customtab2.setBackgroundResource(R.color.unselected_tab_color);
}
else if(tab.getPosition()==1)
{
tabText_customtab1.setBackgroundResource(R.color.unselected_tab_color);
tabText_customtab2.setBackgroundResource(R.color.selected_tab_color);

}
}

@Override
public void onTabUnselected(TabLayout.Tab tab)
{
}

@Override
public void onTabReselected(TabLayout.Tab tab)
{
if(tab.getPosition()==0)
{
tabText_customtab1.setBackgroundResource(R.color.selected_tab_color);
tabText_customtab2.setBackgroundResource(R.color.unselected_tab_color);
}
else if(tab.getPosition()==1)
{
tabText_customtab1.setBackgroundResource(R.color.unselected_tab_color);
tabText_customtab2.setBackgroundResource(R.color.selected_tab_color);
}
}
});


Related Topics



Leave a reply



Submit