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?

Regards,

Richard.

--~--~---------~--~----~------------~-------~--~----~



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

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


 Dianne,

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 );

Regards,

Richard.

--~--~---------~--~----~------------~-------~--~----~


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?

Regards,

Richard.

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. nyobain android market web

kalau dikasih market enabler dulu, kira2 terus bisa akses ndak ya?

Salam,
Andri





-- 
===============

2. [Wts] desire HD bnib

Permisi, mau numpang gelar lapak..

Desire HD BNIB garansi singapore (bisa dibantu claim)
Harga 5,750

HD2 2nd

Samsung galaxy s 2nd

Nexus one 2nd

Yg berminat bisa hubungi mony...@robotijo.org 

Harga include ongkir 1hari sampe+packing kayu+asuransi shipment via RPX/JNE 

Thanks jalumnya..
sent from my IPhone 4

-- 
===============

3. Direction GPS pake voice

4. How to fetch own mobile number in android

5. TableLayout getting invalidated

6. TableLyaout getting invalidated

7. SKIA Bitmap loading ??