This is my code: It's a tabLayout that I setupWith
a Viewpager
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
app:tabPaddingEnd="-1dp"
app:tabBackground="@drawable/tab_color_selector"
app:tabPaddingStart="-1dp"
app:tabTextAppearance="@style/MineCustomTabText" />
But how do I set this programmatically?
app:tabBackground="@drawable/tab_color_selector"
It is really important to set this tabBackground programmatically because I want the color to change depending on the theme that the user has chose
These are what I've already tried but none of them is working:
tabLayout.setBackground(getResources().getDrawable(R.drawable.tab_color_selector));
tabLayout.setBackgroundResource((R.drawable.tab_color_selector));
tabLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_color_selector));
tabLayout.setBackground(ContextCompat.getDrawable(this, R.drawable.tab_color_selector));
Note:
This is my tab_color_selector.xml in drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white" android:state_selected="true" />
<item android:drawable="@color/blue_alu" />
</selector>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…