android – How can I set TextColor, TextSize of PagerTitleStrip from code?-ThrowExceptions

Exception or error:

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.

How to solve:

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.

###

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);

###

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" />

###

Look in this

final TypedArray a = context.obtainStyledAttributes(attrs, ATTRS);  
final int textColor = a.getColor(1, 0);  
final int textSize = a.getDimensionPixelSize(2, 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>

Leave a Reply

Your email address will not be published. Required fields are marked *