1

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 different styles). Then the list item is selected I want to call setTheme(R.style.Own_App_Style0); to change the complete appearance.

The problem is best explained by an example: Lets say we have 2 TextView.

Theme1 1. TextView: TextColor should be green and TextSize 15sp. 2. TextView: TextColor should be red and TextSize 10sp.

Theme2 1. TextView: TextColor should be blue and TextSize 10sp. 2. TextView: TextColor should be yellow and TextSize 10sp.

Of course I know that by setting <item name="textViewStyle">@android:style/Widget.TextView</item> I can change the default appearance of TextViews. But how can it be done to have lets say two (ore more) different types of TextView with different applied styles (and by xml)?

KarlKarlsom
  • 5,868
  • 4
  • 29
  • 36
  • see the answer to this question: http://stackoverflow.com/questions/4630440/how-to-change-a-textviews-style-at-runtime – elijah Jan 06 '12 at 17:19
  • not excactly. I would like to later in the programm only call setTheme(R.style.OwnAppStyle); to change the complete layout. Not change separate widgets problematically when they occur. I added this to the question. – KarlKarlsom Jan 06 '12 at 17:31

2 Answers2

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-styleable name="CustomTextView">
    <attr name="mainTextView" format="reference"/>            
</declare-styleable>

<style name="appstyle0" parent="android:style/Theme.Holo.Light">
    <item name="@attr/mainTextView">@style/CustomTextViewAppearance1</item>
    <item name="android:textViewStyle">@style/CustomTextViewAppearance2</item>
</style>

<style name="appstyle1" parent="android:style/Theme.Holo.Light">
    <item name="@attr/mainTextView">@style/CustomTextViewAppearance2</item>
    <item name="android:textViewStyle">@style/CustomTextViewAppearance1</item>
</style>
<style name="CustomTextViewAppearance1">
    <item name="android:textSize">10dip</item>
</style>
<style name="CustomTextViewAppearance2">
    <item name="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:

        <TextView
            android: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.

Community
  • 1
  • 1
KarlKarlsom
  • 5,868
  • 4
  • 29
  • 36
  • thanks, that helps me! I was expecting to have something like a "selector" at first: 10dip – dalf Mar 27 '14 at 13:09
0

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.

elijah
  • 2,904
  • 1
  • 17
  • 21