views:

1732

answers:

3

Using the Android SDK, there doesn't seem to be any way to set the style on a table row. I want to do the equivalent of:

<TableRow
    style='@style/TableRow_classic' >

in code. I would like something like:

TableRow row = new TableRow(this);
row.setStyle(R.style.TableRow_classic);

Does anyone know how to find this?

+1  A: 

Setting styles is a little weird in Android. Here's the link that I found most helpful: Applying Styles and Themes. I've never set a style, but I've set other attributes of XML elements, so I think that this might be a good approach:

  1. make a style in a file (ie. style.xml)
  2. get a reference in Java to the element you wish to add the style to:

    // I'm setting the style on a TextView with id: myTextView
    TextView toBeStyled = (TextView) findViewById(R.id.myTextView);

  3. set the style on the element:

    // style.xml may need to be accessed with findViewById(R.id.style.xml). toBeStyled.setStyle(style.xml); // if you wanted to set the toBeStyled element to be invisible: toBeStyled.setVisibility(View.INVISIBLE);

Again, I'm not sure that this will work, but between this and the link, it should be a good start.

the Will Cole
Yeah, it works. The file must be styles.xml under res/values.
stevedbrown
Good to know. Since I just started on this forum, I would really appreciate if you'd select my answer if it was sufficient.
the Will Cole
Why, thank you very much.
the Will Cole
WTF? This doesn't work; there's no View.setStyle() function and no R.id.mystyle.xml id. Correct answer here: http://stackoverflow.com/questions/2016249/how-to-programmatically-setting-style-attribute-in-a-view
Timmmm
@Tim: There isn't a View.setStyle, but there is a R.style.xmlFileName
Casebash
A: 

As far as I can tell, there is no setStyle method. You could go through and set each individual property, but that defeats the purpose of separating your View from your controller logic. Bad.

Has anyone figured out how to dynamically assign styles?

A: 

I am trying myself to set style properties obtained from my Theme in code.

My reason is that I want to use a style from my own Theme, BUT my User Interface Layout is entirely generated in code( using a custom layout builder), without defining any widgets in XML. So I cannot set a style in the XML layout of my widget – there isn’t any XML layout.

I am thinking that I will be able to set this style in the code of my widget 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

verylongword