Change the color of the tab indicator in the action bar

I know this question has been asked many times before, but I've tried all solutions and nothing seems to work. So I decided to follow the official documentation: https://developer.android.com/training/basics/actionbar/styling.html

I created a themes.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
        parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:actionBarTabStyle">@style/MyActionBarTabs</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>
    </style>

    <!-- ActionBar tabs styles -->
    <style name="MyActionBarTabs"
        parent="@android:style/Widget.Holo.ActionBar.TabView">
        <!-- tab indicator -->
        <item name="android:background">@drawable/actionbar_tab_indicator</item>
    </style>
</resources>

      

actionbar_background is a simple solid fill form and it works.
However, the indicator is still blue: the
actionbar_tab_indicator is a file generated with http://jgilfelt.github.io/android-actionbarstylegenerator/ I also copied the generated images into an outputable folder. The first problem is that the 9 patch files were somehow wrong, but I managed to manually fix them.

I am customizing the theme in AndroidManifest.xml: android: theme = "@ style / CustomActionBarTheme"

I have also tried solutions to change the color programmatically, but it does not work as expected. Instead of a small rectangle, it stretches to fit the entire tab.

Any solution (programmatically or in xml) working in latest android studio would be great. But it should look just like the original tab, but with a different color - in my case, red.

Hello

+3


source to share


5 answers


I have always found it easier to use the tools from Android Asset Studio since I am not particularly graphic-minded. This tool, in particular, is very important for what you are trying to accomplish: http://jgilfelt.github.io/android-actionbarstylegenerator/.The full toolbox can be found at http://romannurik.github.io/AndroidAssetStudio/ . Just select the colors / styles you want, download the zip file and copy / paste the files into your project. Then, in your AndroidManifest.xml file, include the "Style Name" link you select in the generator (the first text box you filled in on the website) in the action that contains your ActionBar. For example, if you called your "Custom_action_bar" style in the generator, you would add it to your activity like this:



<activity
    android:name="com.company.appname.ActivityName"
    android:label="@string/somename"
    android:theme="@style/Theme.Custom_action_bar">
    <!--the above line is all you need to add-->
</activity>

      

+2


source


Can you try:

<style name="MyActionBarTabs"  
              parent="@android:style/Widget.Holo.Light.ActionBar.TabView">
    <item name="android:background">@drawable/actionbar_tab_indicator</item>

</style>

      



because of your parent="@android:style/Theme.Holo.Light.DarkActionBar"

forCustomActionBarTheme

0


source


First of all, define one selector in the hyphenable folder (tab_indicator.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_selected="true" android:drawable="@drawable/background_tab_selected" />

<item android:state_selected="false" android:drawable="@drawable/background_tab_unselected" />

</selector>

      

@ drawable / background_tab_selected and @ drawable / background_tab_unselected are nine-patch images that show the selected and unselected state of the scoreboard respectively.

Note. ... The background_tab_selected scalable area of ​​the nine-patch image should only contain pixels that match the normal background. like this: enter image description here(I mean the left and top indicators with nine patches)

then in your onCreate () of your activity find the TabWidget object and set their background:

final TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs);

tabs.getChildTabViewAt(0).setBackgroundResource(R.drawable.tab_indicator);

tabs.getChildTabViewAt(1).setBackgroundResource(R.drawable.tab_indicator);

tabs.getChildTabViewAt(2).setBackgroundResource(R.drawable.tab_indicator);

tabs.setDividerDrawable(R.drawable.tab_divider);

      

also you can set the separator using setDividerDrawable () method.

Hope it helps.

0


source


Changing the indicator color of the actionBar tab is difficult, instead you can use SlidingTabLayout with which you can dynamically change the color of the indicator, also you can change the height of the indicator.

To get the SlidingTabLayout (& widget) class , import the sample project into Android studio:

SlideTabLayout has various options for customizing tabs, indicator and its text.

Use this in your project and enjoy!

0


source


In your XML file, you can use the following code to change the height of the tabIndicator.

<android.support.design.widget.TabLayout`
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="62dp"
    android:background="#001e8b"
    android:elevation="6dp"
    app:tabSelectedTextColor="@android:color/white"
    app:tabIndicatorHeight="5dp"
    app:tabIndicatorColor="@android:color/white"
    app:tabTextColor="@android:color/darker_gray"`
    />

      

0


source







All Articles