What is the 'correct' way to set styles programmatically?

by Kennard Consulting » Wed, 04 Feb 2009 12:16:09 GMT

Sponsored Links
 Dear All,

Given a widget and a int that references a style (defined in my res/
values/style.xml), what is the 'correct' way to programmatically apply
the style to the widget?

Up until now I've been hacking this by...

protected void applyStyle( View view, int style ) {

        TypedArray attributes = getContext().obtainStyledAttributes( style,
android.R.styleable.View );

        int padding = attributes.getDimensionPixelSize
( android.R.styleable.View_padding, -1 );
        view.setPadding( padding, padding, padding, padding );

...but with the demise of android.R.styleable in 1.0 Release 2, I
guess it's time to implement this properly?

I note:

1. AssetManager.applyStyle is package-protected
2. Theme.obtainStyleAttributes is hardcoded to use XmlBlock$Parser,
not just an AttributeSet

So what is the 'official' way?




What is the 'correct' way to set styles programmatically?

by Kennard Consulting » Wed, 04 Feb 2009 14:35:14 GMT


Thanks for the prompt reply. However, I don't think your answer works
in my case, as what I am trying to do is a little unusual.

I'm trying to apply styles *programmatically*, like the Android
framework does internally using (I think) AssetManager.applyStyle and/
or Theme.obtainStyleAttributes. However, neither of these are exposed
in the public API.

Is there a way, in the public API, to apply a style to a View at
runtime, given the style id and the View object? For example...

    TextView view = new TextView();
    applyStyle( view, R.mystyle );




Sponsored Links

What is the 'correct' way to set styles programmatically?

by Kennard Consulting » Wed, 04 Feb 2009 19:02:10 GMT

 Thanks again for your help, Dianne.

My apologies for not making myself clear: my original code was just a
hack. I would rather not manually retrieve the values and call setters
on a view.

So are you saying if I want to programmatically style a view I must
not use, say, 'new TextBox( context )' but rather 'new TextBox
( context, attributeSet )'? That's okay. In that case is there a way I
can obtain an AttributeSet given a style id?

For example, do I pass a home-made implementation of AttributeSet to
Theme.obtainStyledAttributes() and it will populate it for me? I tried
this in the past but there seemed to be some kind of hard-coded cast
to XmlBlock$Parser inside Theme.obtainStyledAttributes?




Other Threads

1. Help, referensy bongkar miley

Wadoooow... jam piro iq boss? Besok aja loh... ilang ntar baut nya buka
sambil ngantuk2... hahaha...

# sent by Google Own Son #

On Feb 9, 2010 2:17 AM, "iskandar gilaoriginal" <racungad...@gmail.com>

as subject ,rekan mohon bertanya sekaligus bantuanya,apakah ada yg pernah
atau tau info tentang cara membongkar body si miley? Miley saya lcdnya
blank,namun kpad dalam dan dpn menyala..lampu lcd padam...soalnya jaringan
rewel jd ga bisa browsing2.uda gatel pengen bedah aja...thk u
Gila original
originalbranded accessories
plasa marina lt 2 h 267
surabaya 031 91555898
ym: gilaoriginal
gtalk: racungadget

"Indonesian Android Community [id-android]"

2. Terbangun dari mimpi?

Just a crazy idea, will google make a email client? or even Google  

bayangkan bisa sync contact automatic dengan multiple account gmail kita dan bisa di merge pula ke 1 account yang bisa kita tentukan. demikian juga untuk kalender, dan semuanya di lakukan OTA document bisa di sync dari HD kita ke account google kita hmmm mantapzzzz dan pastinya, semua di akses via android oled berlayar 4" processor 1.5G, RAM 1G, ROM 1G, Full sliding and tilt qwerty keyboard, solid state 32G "No more privacy, google will rule the world hahahahaha" ~...@greenbot -- "Indonesian Android Community [id-android]"

3. hTC Legend with Android 2.1 and Optical Mouse

4. Adjusting contents of ListView at runtime

5. SQlite error

6. Gathering, Official Tshirt id-android, WTI: Komunitas id-android di Majalah Mobile Guide hal 28

7. Adjusting contents of ListView at runtime