Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set TextColor, TextSize of PagerTitleStrip from code?

Tags:

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.

like image 782
Андрей Avatar asked Mar 29 '12 12:03

Андрей


2 Answers

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.

like image 144
bhaskarc Avatar answered Oct 14 '22 04:10

bhaskarc


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);
like image 43
Budius Avatar answered Oct 14 '22 04:10

Budius