Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of TabIndicater in PagerTabStrip

I have an android.support.v4.view.PagerTabStrip defined in xml as follows :

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--
    This title strip will display the currently visible page title, as well as the page
    titles for adjacent pages.
    -->
    <android.support.v4.view.PagerTabStrip android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:textColor="#fff"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" />

</android.support.v4.view.ViewPager>

How can I change the TabIndicater color? It can be changed programmaticaly by setTabIndicatorColor(int color) but I need to find a way to do this in xml.

like image 608
Binoy Babu Avatar asked Nov 07 '12 03:11

Binoy Babu


1 Answers

The main method is to make a class extends PagerTabStrip, for example,the class named MyPagerTabStrip:

package com.example.View; /// change this package to yourselves.

public class MyPagerTabStrip extends PagerTabStrip
{
    public MyPagerTabStrip(Context context, AttributeSet attrs)
    {
    super(context, attrs);
    final TypedArray a = context.obtainStyledAttributes(attrs,
                                  R.styleable.MyPagerTabStrip);
    setTabIndicatorColor(a.getColor(
            R.styleable.MyPagerTabStrip_indicatorColor, Color.BLUE));
    a.recycle();
    }

}

in res/values folder new a file named attrs.xml, the content of this file is:

<?xml version="1.0" encoding="utf-8"?>

<declare-styleable name="MyPagerTabStrip">
    <attr name="indicatorColor" format="reference|color" />
</declare-styleable>

The same folder, new a file if not exists, named styles.xml, put the code below in the file:

<style name="Pager">
    <item name="android:textColor">#ffffff</item>
    <item name="indicatorColor">#6f8dc5</item>
</style>

Finally, your layout xml file is :

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<com.example.view.MyPagerTabStrip android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#33b5e5"
    android:textColor="#fff"
    android:paddingTop="4dp"
    android:paddingBottom="4dp" />

like image 113
facebook-100000005252130 Avatar answered Oct 14 '22 12:10

facebook-100000005252130