How to set TextStyle of PagerTabStrip to Bold?

16,143

Solution 1

What you need to do is create a style for the text, and then use the android:textAppearance attribute...

Perhaps something like this:

 <style name="PagerTabStripText">
      <item name="android:textSize">14sp</item>
      <item name="android:textStyle">bold</item>
      <item name="android:textColor">#a4c639</item>
 </style>

And then in your PagerTabStrip XML do this:

 <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" 
        android:textAppearance="@style/PagerTabStripText"/>

Solution 2

PagerTabStrip doesn't aceppts textAppearance.

Like @Justin said above, but on xml put:

style="@style/PagerTabStripText"

not:

android:textAppearance="@style/PagerTabStripText"

Solution 3

If you'd like to style the center tab title differently from the others, then you'll need to use reflection:

try {
    final Class pagerTitleStripClass = PagerTitleStrip.class;
    final Field mCurrTextField = pagerTitleStripClass.getDeclaredField("mCurrText");
    mCurrTextField.setAccessible(true);

    // mTitle is my class's PagerTabStrip member variable
    final TextView mCurrText = (TextView) mCurrTextField.get(mTitle);
    mCurrText.setTypeface(Typeface.DEFAULT_BOLD);
} catch (final Exception e) {
    Log.w(TAG, "Exception when styling currently selected title!", e);
}

Unfortunately, this solution is subject to potentially not working anymore when updates for the Android Support Library are released (as that's where the PagerTitleStrip class is from). In particular, this code sample is working with revision 20.

Share:
16,143

Related videos on Youtube

Jainendra
Author by

Jainendra

Send me a direct message

Updated on August 22, 2022

Comments

  • Jainendra
    Jainendra over 1 year

    I'm using PagerTabStrip in ViewPager to show the title of each page. Here is my XML code:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:background="@drawable/bg">
    
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
        >
        <android.support.v4.view.PagerTabStrip
                android:id="@+id/pager_title_strip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:background="#33b5e5"
                android:textColor="#a4c639"
                android:paddingTop="4dp"
                android:paddingBottom="4dp" />
        </android.support.v4.view.ViewPager>
    
    </RelativeLayout>
    

    I want to set the textStyle to bold in PagerTabStrip like this: android:textStyle="bold". But this is not possible in PagerTabStrip. Seems like it has only textcolor property for text. What is the way by which I can set the style to bold?

  • Eugene Chumak
    Eugene Chumak about 11 years
    By the way, eclipse doesnt autosuggest android:textAppearance inside android.support.v4.view.PagerTabStrip. But dont be confused by that, textAppearance works for PagerTabStrip.
  • ambit
    ambit about 11 years
    I need to use custom typeface for my pagertabstrip. Any idea how to use that?
  • Braj
    Braj over 9 years
    @Justin - textMultiLine property is not working in this way. Any other way around?
  • Charles Madere
    Charles Madere over 9 years
    Any idea on how one specifically styles the center tab title? As in, the user's current tab title would be bold and the others be normal?
  • therealprashant
    therealprashant over 8 years
    android studio doesnt have textAppearance
  • kamathln
    kamathln over 6 years
    I personally wouldn't recommend using reflection for these kind of "tricks". There is a reason it is private - It may change at any time - even during different commits of the same revision. Reflection should be used for meta-level-stuff like writing frameworks, and such.