0

As mentioned in topic, I have some Views, e.g. a TableRow with always the same background used as topic, or a special TableRow containing a TextView with some special styles/properties. These Views are set dynamically, so it's problematic to use a XML for this. As I read it's not possible to set styles programmatically too. So what's the best way to solve that?

Possibility 1:
I use and instance derived Views, like this:

public class TopicTableRow extends TableRow {

    public TopicTableRow(Context context) {
        super(context);

        setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        setBackgroundColor(Color.parseColor("#777777"));
        setClickable(false);
    }
} 

Possibility 2:
I could create a valid xml template with a special layout I never use in the application, containing the needed Views which have already all assigned styles. Afterward I access the needed Views by R.id....
But this method seems to be very dilettante to me.

I don't think that those 2 possibilities are the "real" Android way to do this, so how is this usually done?

Bevor
  • 8,396
  • 15
  • 77
  • 141
  • http://stackoverflow.com/questions/2016249/how-to-programmatically-setting-style-attribute-in-a-view – Yury Dec 27 '11 at 14:55
  • And how should I use this when I dynamically create elements which are not defined in XML? – Bevor Dec 27 '11 at 14:58
  • You cannot change styles for dynamic elements. This is the main point of the comment. – Yury Dec 27 '11 at 15:01
  • I don't want to change/set styles dynamically. I want to create reusable elements with special properties/styles (what ever). – Bevor Dec 27 '11 at 15:04
  • Sorry, I at first did not understand your question. I think in this case you can extend class view for this purposes. Maybe the following example will help you. There a guy creates custom scrollable table. http://blog.stylingandroid.com/archives/432 Hope this will help you. – Yury Dec 27 '11 at 15:19

1 Answers1

0

If you want to set specific styles for groups of elements, you can use the themes and styles concepts in android.

You can read up on them here: http://developer.android.com/guide/topics/ui/themes.html

It is not possible though to change the style attribute of a view programatically.

Therefore the android way is probably to create the Views you need in XML and use a LayoutInflater to get create an 'java' version of the xml view. This allows you to reuse the component and fill it with apropriate data for as many rows as you would like.

Button view = (Button) LayoutInflater.from(this).inflate(R.layout.textViewFromWeb, null);

I hope this will be of use to you!

RandomSort
  • 600
  • 7
  • 22
  • I already thought about this, but they assign a style to a View defined in an XML. In my case I don't have my Views defined in XML, because I create them dynamically due to received data, which leads me to my 2nd proposal to create a "dummy layout" containing the special view which I can access in code with R.id. But as I already said, this doesn't look very clean to me. – Bevor Dec 27 '11 at 15:35
  • I have edited a bit to add the dynamically created buttons. When you inflate the view you create a copy of it in java, so you can use this method to create how ever many you would want or with what content you want. – RandomSort Dec 27 '11 at 20:44
  • Althought everything is not clear yet, I think that I'm on the right track now. The key information is that I have to copy the right "raw layout" from /platforms//data/res/layout/ into my own project. From there I can use it with ArrayAdapter and LayoutInflater to modify its view while building the dynamic list. Although your answer wasn't clear for me at first, it led me to the right information. – Bevor Dec 28 '11 at 18:33