20

I am trying to set the textsize and textcolor from the pagertitlestrip and cannot find a way to do it.

<android.support.v4.view.ViewPager
    android:id="@+id/myPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/myStrip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top" />

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

EDIT

main.xml

<android.support.v4.view.ViewPager
    android:id="@+id/myPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.PagerTitleStrip
        style="@style/PagerTitleStrip"
        android:id="@+id/myStrip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top" />

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

style.xml

<style name="PagerTitleStrip">
    <item name="android:textAppearance">@style/PagerTitleStripTextAppearance</item>
</style>

<style name="PagerTitleStripTextAppearance">
    <item name="android:textColor">#FFFFFFFF</item>
    <item name="android:textSize">14sp</item>
</style>

Pity that i didn't found a way to make it from AttributeSet, get a Resource NotFound or InvalidResource Exception.

1
  • Do you want to set only those attributes for the text in the PagerTitleStrip?
    – user
    Jun 22, 2012 at 14:38

5 Answers 5

32

textSize is not available directly as XML property of view.PagerTitleStrip.

However I managed to set it up indirectly using the style attribute as follows:

<android.support.v4.view.PagerTitleStrip
    android:id="@+id/pager_title_strip"
    style="@style/viewPagerTitleStrip" <-- added style attribute here
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
     />

Then I defined the textSize in styles.xml as follows:

<style name="viewPagerTitleStrip">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:textSize">14sp</item> <-- my desired text size
</style>

This worked for me.

2
  • This is the best way!
    – User3
    Mar 24, 2015 at 10:43
  • 1
    @Андрей - This is working. And demands the "Correct Check" as the correct answer. Thanks all. Jun 9, 2016 at 20:38
10

from: http://developer.android.com/reference/android/support/v4/view/PagerTitleStrip.html

Set the color value used as the base color for all displayed page titles:

void setTextColor(int color) 

Set the default text size to a given unit and value:

void setTextSize(int unit, float size) 

looks like a no-brainer.

PagerTitleStrip titleStrip = (PagerTitleStrip) findViewById(R.id.titleStrip);
titleStrip.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 24);
0
5

I stumbled upon this question really late, but as of now, textSize though not in the prediction option, works.

<android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="top"
        android:background="@android:color/holo_blue_dark"
        android:textSize="18sp"

        android:textColor="#ffffff"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" />
1
  • Still works perfectly in Android Studio 3.2.x running on Nugat image and in 2019. Thanks!
    – raddevus
    Mar 2, 2019 at 23:59
2

Look in this

final TypedArray a = context.obtainStyledAttributes(attrs, ATTRS);  
final int textColor = a.getColor(1, 0);  
final int textSize = a.getDimensionPixelSize(2, 0);
7
  • 1
    public PagerTitleStrip(Context context, AttributeSet attrs) { super(context, attrs); (...) } Any example to create AttributeSet for PagerTitleStrip ??
    – CONDEVX
    Mar 29, 2012 at 15:38
  • i wrote this: XmlPullParser parser = getResources().getXml(R.xml.mypagertitlestripattributes); AttributeSet attributes = Xml.asAttributeSet(parser); pagerTitleStrip = new PagerTitleStrip(this, attributes); pagerTitleStrip = (PagerTitleStrip) findViewById(R.id.myStrip); and the xml <?xml version="1.0" encoding="utf-8"?> <resources> <item name="android.R.attr.textAppearance">@null</item> <item name="android.R.attr.textColor">#FFFF0000</item> <item name="android:textSize">30sp</item> </resources> but nothing changes no crash no exception
    – CONDEVX
    Mar 29, 2012 at 16:52
  • Do you have a PagerTitleStrip in xml layout? Mar 29, 2012 at 17:39
  • But you ask in title to do it from code. look in sources of PagerTitleStrip for attributes names and put them to layout. Also look in you code above! You did very easy mistake Mar 29, 2012 at 17:51
0

textsize, text color not applied to tab. My code is as follow:

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/relativeLayout">

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

    <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="5dp"`enter code here`
        android:paddingBottom="5dp" />

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.