How do I change a tab background color when using TabLayout?

This is my code in the main activity

public class FilterActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_filter);

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager(), FilterActivity.this);
    viewPager.setAdapter(pageAdapter);

    // Give the TabLayout the ViewPager
    final TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
    tabLayout.setupWithViewPager(viewPager);



  }
}

And this is my code in the XML

  • How to set layout dynamically in android
  • How to close navigation drawer when an item is pressed from it?
  • To access shared SharedPreference from service in android
  • Android: Forcing external microphone (jack input 3.5mm) to stay on
  • Copy directory from Assets to local directory
  • android set style in code
  • <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <include
            android:id="@+id/app_bar"
            layout="@layout/app_bar">
        </include>
    
        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="fill_parent"
            style="@style/MyCustomTabLayout"
            android:layout_height="48dp"/>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1"
            android:background="@android:color/white" />
    
    </LinearLayout>
    

    I want to change the background color of one tab when it’s selected

    Related posts:

    Execution failed for task &#039;app:mergeDebugResources&#039; Crunching Cruncher&hellip;...
    Could not open Selected VM debug port (8700)
    NavigationView OnNavigationItemSelectedListener not being called
    Android Lollipop - changed behavior of SQLite
    Genymotion devices not working in Eclipse on OS X Yosemite
    Class Not Found Exception when Running YouTube API Demos for Android
  • Android Wear notification prevent blinking icon when updating
  • Adding extra images/icons to CardFragment (Android Wear)
  • Using Parcelable with circular references
  • Android Service never appears to start - onStartCommand() not called
  • zoom in phonegap for android
  • Android annotations and applicationIdSuffix
  • 4 Solutions collect form web for “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>
    

    You can try this:

    <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
        <item name="tabBackground">@drawable/background</item>
    </style>
    

    In your background xml file:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:drawable="@color/white" />
        <item android:drawable="@color/black" />
    </selector>
    

    Add atribute in xml:

    <android.support.design.widget.TabLayout
        ....
        app:tabBackground="@drawable/tab_color_selector"
        ...
        />
    

    And create in drawable folder, 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>
    

    Have you tried checking the API?

    You will need to create a listener for the OnTabSelectedListener event, then when a user selects any tab you should check if it is the correct one, then change the background color using tabLayout.setBackgroundColor(int color), or if it is not the correct tab make sure you change back to the normal color again with the same method.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.