Question

Well, I have a problem with setTextAppearance when apply a style to the TextView object at runtime.

I defined this in the layout:

<TextView style="@style/Proximity_Far" android:id="@+id/tvNearFar" />

In /res/values/styles.xml I defined this:

<style name="Proximity">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:gravity">center</item>
    <item name="android:textStyle">bold</item>
</style>
<style name="Proximity_Near" parent="Proximity">
    <item name="android:textSize">140dp</item>
    <item name="android:textColor">#ff00ff05</item>
    <item name="android:text">Near!</item>
</style>
<style name="Proximity_Far" parent="Proximity">
    <item name="android:textSize">70dp</item>
    <item name="android:textColor">#ffff0079</item>
    <item name="android:text">Far!</item>
</style>

You can see that I have set the text in each style Proximity_Near and Proximity_Far. So then I call the function setTextAppearance in order to change style and also the display text of tvNearFar on the view:

tvNearFar.setTextAppearance(getApplicationContext(), R.style.Proximity_Near);

But this only affects on the text style, the text content stays unchanged. Anyone can explain me about this? I'm working on Android Studio, and I can see that change when I change the style id in layout file at design time, but it doesn't work at runtime.

Any help would be appreciated.

Was it helpful?

Solution

From the Android developer documentation:

public void setTextAppearance (Context context, int resid)

Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.

It's not supposed to change the String. "Style" in this case means textStyle bold/normal/italic.

You'll have to set the String separately to achieve what you wanted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top