Search code examples
androidtabitem

Blue color background TabItem when pressed


I'm beginner Android programmer. I create a TabView and when pressing any item in tab Items the color should be gray, but it becomes blue. Previously, I encountered this problem at Navigation Bottom Menu. I'm confused. Where is the problem? I've put two screenshots at the end to explain what I want and what is happening.

This is my layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_above="@id/tabs"
        android:layout_alignParentTop="true" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ثبت نقدی" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="پیامک بانکی" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="نمودار" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="بیشتر" />

    </android.support.design.widget.TabLayout>

</RelativeLayout>

In the images bellow button پیامک بانکی is pressed.

This is what I want:

This is what I want

This is my problem. I do not know where the blue color comes from.

This is my problem. I do not know where the blue color comes from.


Solution

  • I think you can create customer selector. Go to drawable folder and right click. Then select New->create DrawableResource. Add the file name as tab_selector. Then add below code to that file

        <?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>
    

    Got to value->color file and add these two colors

    <color name="tab_background_selected">#991c47</color>
    <color name="tab_background_unselected">#4d8e63</color>
    

    You can customize these colors based on your preferences.

    Then add app:tabBackground="@drawable/tab_selector" to tab layout view

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabBackground="@drawable/tab_selector"
        android:layout_alignParentBottom="true">