Skip to content Skip to sidebar Skip to footer

Different Formats For Different Textview Defined By Custom Style

I have a problem figuring out how to do this: I am currently coding an app that comes with different themes (User can select the complete appereance of the app out of a list of dif

Solution 1:

Found a solution (basically in this answer setTextAppearance through code referencing custom attribute). In case anyone else has this problem I shortly explain:

Declare in style.xml a attribute and in the actual style definition asign a value (reference) to this attribute:

<declare-styleablename="CustomTextView"><attrname="mainTextView"format="reference"/></declare-styleable><stylename="appstyle0"parent="android:style/Theme.Holo.Light"><itemname="@attr/mainTextView">@style/CustomTextViewAppearance1</item><itemname="android:textViewStyle">@style/CustomTextViewAppearance2</item></style><stylename="appstyle1"parent="android:style/Theme.Holo.Light"><itemname="@attr/mainTextView">@style/CustomTextViewAppearance2</item><itemname="android:textViewStyle">@style/CustomTextViewAppearance1</item></style><stylename="CustomTextViewAppearance1"><itemname="android:textSize">10dip</item></style><stylename="CustomTextViewAppearance2"><itemname="android:textSize">30dip</item></style>

Now in the layout all textViews are like CustomTextViewAppearance2 (because this is set as standard in this style. And the textViews that should use the other style write into the definition:

<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="blablabla"style="?mainButtonTextView"/>

When you now call .setTheme (after restart the activity) the appearance of the textviews switch. Like this method you can define as many different types of View styles and switch between them only by calling .setTheme.

Solution 2:

Unfortunately, styles are static once they are defined. To have an entire cascade of styles modified programmatically, you would have to change the definition of the style itself. Instead, all you can do is change the style that is assigned to a TextView (or whatever style-able object), as outlined in the question I linked to in my comment above.

Post a Comment for "Different Formats For Different Textview Defined By Custom Style"