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. Confusing IllegalArgumentException from openFileOutput

I'm trying to create a new file in my data area, and I'm getting this
exception:

01-12 13:59:41.202: ERROR/AndroidRuntime(1437):
java.lang.IllegalArgumentException: File
/data/data/com.casadelgato.lifewallpaper/files/LifeSettings.ini contains a
path separator

As far as I can tell, that's a valid file spec.

-- 

2. SensorManager failing to start listener thread

Hi,

I am porting android to a custom hardware platform where we will have a 
variety of sensors. As a first step I wrote a sensor library and a simple 
application to test getting data from the sensors.

The problem is that when I run my application, the SensorManager listener 
thread never gets launched as seen in the de{*filter*}, and so I never get any 
data.

I traced the problem down to this call which fails:

from SensorManager.java:
        // must be called with sListeners lock
        boolean startLocked(ISensorService service) {
            try {
                if (mThread == null) {
                    Bundle dataChannel = service.getDataChannel();
                    if (dataChannel != null) {
[...]

The call to service.getDataChannel() seems to fail and I see !!! FAILED 
BINDER TRANSACTION !!! in logcat.

I am new to android, so I don't understand what could be happening here...

Can anyone suggest what to look for next?
--
Regards, Devin

-- 

3. Soft keyboard alters layout

4. DynaDroid4i - New Open Source Project

5. Bluetooth API & Device Power Modes

6. Android Twitter client to share Images

7. about content provider physical store location