4

I've describef properties of my objects (nevermind what the object is) in styles.xml. I would like to change these properties dynamically in styles.xml.

Does anybody know how can I do that?

brettdj
  • 54,857
  • 16
  • 114
  • 177
davs
  • 9,226
  • 8
  • 41
  • 54

3 Answers3

7

You can do that like:

In Activity:

this.setTheme(R.style.ThemeRed);

In styles.xml:

<resources>
  <style name="ThemeBlack" parent="@android:style/Theme">
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:typeface">sans</item>
    <<item name="android:background">#999999</item>
    <item name="android:textSize">16sp</item>
  </style>
  <style name="ThemeRed" parent="@android:style/Theme">
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:typeface">sans</item>
    <item name="android:background">#c81111</item>
    <item name="android:textSize">16sp</item>
  </style>
</resources>
  • This is interesting, but it change described properties for all subViews of _current_ _ACTIVITY_ (but I need only for specific Views); – davs Mar 07 '11 at 09:46
3

I am seeing contradicting answers to this question. This says yes, but this says no. I looked at the android docs for the View and can not find any setStyle method.

Community
  • 1
  • 1
Ryan Conrad
  • 6,870
  • 2
  • 36
  • 36
  • 1
    Agreed, there is no `setStyle()` method. I know of no way to change your style at runtime. You can change most of the properties set by a style at runtime, but you have to do that property-by-property. – CommonsWare Jun 21 '10 at 13:14
0

I would like to change these properties dinamically in styles.xml.

What exactly do you want to change? The style applied at runtime?

I am thinking you can do this in your code by using

TypedArray a =  context.obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)

Here it seems (to me) that

  1. AttributeSet set = null; because this is what the XML inflater would have provided.

  2. int[] attrs = R.styleable.MyWidget; defines what attributes I want to look at.

  3. int defStyleAttr = myWidgetStyle; which is a reference, defined in my Theme, to a style for MyWidget. These are both defined in XML files in res/values. “myWidgetStyle” follows the pattern of name the android developers have used in their code.

  4. defStyleRes = 0; I am hoping that I don’t need to think about this.

Then to get any property , such as a background color,

Color  color = a.getColor(R.styleable.MyWidget_background,  R.color.my_default);
a.recycle();

This does seem to work –so far anyway.

It seems that the android build system conveniently generates the correct index to use in a.getColor, and names it R.styleable.MyWidget_background. I didn't make this name, so Android must have done it using my XML for my styleable MyWidget.

I expect one can look up the correct index by searching the TypedArray for the required attribute , but that would be inefficient and the TypedArray looks like an unpleasant contraption to deal with. I would use a very long stick to poke it!

Don

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150