java - How to change the color of icon of the selected tab of TabLayout?

Java - How to change the color of icon of the selected tab of TabLayout?

To change the color of the icon of the selected tab in a TabLayout in Android using Java, you can create a custom tab layout with a custom layout for each tab. Then, you can programmatically change the color of the icon based on the selected state of the tab.

Here's how you can do it:

  1. First, create a custom layout for your tab with an ImageView for the icon:
<!-- tab_custom_layout.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/tab_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <!-- Add other views if needed, like TextView for title --> </LinearLayout> 
  1. Use this custom layout for your tabs when setting up the TabLayout:
// Set up TabLayout TabLayout tabLayout = findViewById(R.id.tab_layout); tabLayout.setupWithViewPager(viewPager); // Set custom view for each tab for (int i = 0; i < tabLayout.getTabCount(); i++) { TabLayout.Tab tab = tabLayout.getTabAt(i); if (tab != null) { tab.setCustomView(R.layout.tab_custom_layout); // You can also set title or other views here if needed } } 
  1. Finally, programmatically change the color of the icon based on the selected state of the tab:
// Listen for tab selection change tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { // Get the custom view for the selected tab View customView = tab.getCustomView(); if (customView != null) { // Find the ImageView and change its color ImageView iconImageView = customView.findViewById(R.id.tab_icon); if (iconImageView != null) { iconImageView.setColorFilter(ContextCompat.getColor(context, R.color.selected_tab_icon_color), PorterDuff.Mode.SRC_IN); } } } @Override public void onTabUnselected(TabLayout.Tab tab) { // You can reset the color here if needed } @Override public void onTabReselected(TabLayout.Tab tab) { // Optional: Handle reselection of the tab } }); 

In this code:

  • We use a custom layout tab_custom_layout.xml for each tab, which includes an ImageView for the icon.
  • We set this custom layout for each tab using tab.setCustomView(R.layout.tab_custom_layout).
  • We listen for tab selection changes using tabLayout.addOnTabSelectedListener() and programmatically change the color of the icon based on the selected state using ImageView.setColorFilter().
  • Don't forget to define the color resource selected_tab_icon_color in your res/values/colors.xml file.

Adjust the icon color, layout, and other properties as needed for your application.

Examples

  1. Change Tab Icon Color on Selection in TabLayout

    • Description: This snippet demonstrates how to use a TabLayout.OnTabSelectedListener to change the icon color of the selected tab.
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { tab.getIcon().setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_IN); } @Override public void onTabUnselected(TabLayout.Tab tab) { tab.getIcon().setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_IN); } @Override public void onTabReselected(TabLayout.Tab tab) { // Do nothing } }); 
  2. Change Tab Icon Color with ViewPager in TabLayout

    • Description: Integrating TabLayout with ViewPager to change the tab icon color based on the selected page.
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) { @Override public void onTabSelected(TabLayout.Tab tab) { super.onTabSelected(tab); tab.getIcon().setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_IN); } @Override public void onTabUnselected(TabLayout.Tab tab) { super.onTabUnselected(tab); tab.getIcon().setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_IN); } }); 
  3. How to Set Custom View for Tab in TabLayout

    • Description: Using a custom view to control the appearance of the tab, including icon color changes.
    for (int i = 0; i < tabLayout.getTabCount(); i++) { TabLayout.Tab tab = tabLayout.getTabAt(i); if (tab != null) { tab.setCustomView(R.layout.custom_tab); } } tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { View view = tab.getCustomView(); if (view != null) { ImageView icon = view.findViewById(R.id.tab_icon); icon.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_IN); } } @Override public void onTabUnselected(TabLayout.Tab tab) { View view = tab.getCustomView(); if (view != null) { ImageView icon = view.findViewById(R.id.tab_icon); icon.setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_IN); } } @Override public void onTabReselected(TabLayout.Tab tab) { // Do nothing } }); 
  4. Using XML to Change Tab Icon Color in TabLayout

    • Description: Defining a color selector in XML to change the icon color of the selected tab.
    <!-- res/color/tab_icon_color.xml --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#FF0000" android:state_selected="true" /> <item android:color="#000000" /> </selector> 
    for (int i = 0; i < tabLayout.getTabCount(); i++) { TabLayout.Tab tab = tabLayout.getTabAt(i); if (tab != null) { Drawable icon = tab.getIcon(); if (icon != null) { icon.setColorFilter(getResources().getColorStateList(R.color.tab_icon_color), PorterDuff.Mode.SRC_IN); } } } 
  5. Change Tab Icon Color Programmatically in TabLayout

    • Description: Directly setting the color of tab icons programmatically.
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { tab.getIcon().setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_IN); } @Override public void onTabUnselected(TabLayout.Tab tab) { tab.getIcon().setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_IN); } @Override public void onTabReselected(TabLayout.Tab tab) { // Do nothing } }); 
  6. Dynamic Tab Icon Color Change in TabLayout

    • Description: Dynamically changing tab icon color during runtime.
    tabLayout.getTabAt(0).getIcon().setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_IN); tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { tab.getIcon().setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_IN); } @Override public void onTabUnselected(TabLayout.Tab tab) { tab.getIcon().setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_IN); } @Override public void onTabReselected(TabLayout.Tab tab) { // Do nothing } }); 
  7. Changing Tab Icon Color Using Data Binding in TabLayout

    • Description: Utilizing data binding to update the tab icon color.
    <!-- res/layout/custom_tab.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"> <ImageView android:id="@+id/tab_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tab_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tab" /> </LinearLayout> 
    // Binding data in your activity or fragment for (int i = 0; i < tabLayout.getTabCount(); i++) { TabLayout.Tab tab = tabLayout.getTabAt(i); if (tab != null) { tab.setCustomView(R.layout.custom_tab); } } tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { View view = tab.getCustomView(); if (view != null) { ImageView icon = view.findViewById(R.id.tab_icon); icon.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_IN); } } @Override public void onTabUnselected(TabLayout.Tab tab) { View view = tab.getCustomView(); if (view != null) { ImageView icon = view.findViewById(R.id.tab_icon); icon.setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_IN); } } @Override public void onTabReselected(TabLayout.Tab tab) { // Do nothing } }); 
  8. TabLayout Change Icon Color Based on Selected Tab

    • Description: Changing the icon color based on the selected tab state.
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { if (tab.getIcon() != null) { tab.getIcon().setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_IN); } } @Override public void onTabUnselected(TabLayout.Tab tab) { if (tab.getIcon() != null) { tab.getIcon().setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_IN); } } @Override public void onTabReselected(TabLayout.Tab tab) { // Do nothing } }); 
  9. How to Change Selected Tab Icon Color in TabLayout

    • Description: Changing the icon color of the selected tab in a TabLayout.
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { tab.getIcon().setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_IN); } @Override public void onTabUnselected(TabLayout.Tab tab) { tab.getIcon().setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_IN); } @Override public void onTabReselected(TabLayout.Tab tab) { // Do nothing } }); 
  10. **Custom


More Tags

numerical image-resizing screencast stepper jsonschema prepared-statement transfer-learning ringtone graphite compass

More Programming Questions

More Chemistry Calculators

More Electronics Circuits Calculators

More Dog Calculators

More Organic chemistry Calculators