3
I’m developing tabs in Android Studio with com.google.android.material.tabs.Tablayout and would like to change the color of the icon, only of the selected tab.
The icons are in SVG. And the text and icon colors are different, so I can’t use the default style.
API version: 30
Android Studio version: 4.1.1
XML
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="32dp"
tools:ignore="MissingConstraints"
app:tabIndicatorHeight="0dp"
app:tabInlineLabel="true">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ir agora"
android:icon="@drawable/ic_tab_imediatas"
/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ir outro dia"
android:icon="@drawable/ic_tab_programadas"
/>
</com.google.android.material.tabs.TabLayout>
In Activity, I made the following attempt:
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@SuppressLint("ResourceAsColor")
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab.getIcon().setTint(R.color.icon_tab_selected);
//tabLayout.setTabIconTintResource(R.color.icon_tab_selected);
}
@SuppressLint("ResourceAsColor")
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tabLayout.setTabIconTintResource(R.color.icon_tab_unselected);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
But ends up changing the colors of all tabs, and the color is incorrect, it seems that changes only the contrast.
Perfect, just complementing on the different colors, in case someone has the same problem. Following the example of the answer, in the color/tab_content_color_selector.xml I had put
android:drawable
, so it’s the wrong color.– Denner Luan
@Dennerluan as it is... Color is not Drawable. It is a different Source.
– Mateus