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. How to install EAP TLS certificates for WiF

How did you get my email? Why have you contacted me?




thanx to my friend, denver, who suggested this workaround:

1. on your android device enable Credential Storage (settings >>
security & location >> use secure credentials)
2. export your certificate from a windows machine, Base-64 encoded X.
509 (.cer)
start >> run >> certmgr.msc

3. install or just download the zip on your computer: XAMPP Lite
http://www.apachefriends.org/en/xampp-windows.html#646

4. go to: /htdocs and create a new file: certificate.php

<?php
header("Content-Type: application/x-x509-ca-cert");
?>
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----

5. copy the exported certificate content, from step 2, inside
certificate.php -- pay attention to BEGIN -- END
6. from your android phone, remotely access via the web browser your
server:
http://your_xampp_server/certificate.php

7. the import window should appear, type any name you desire
8. the credential storage password should popup, type the password set
on step 1.
9. now go to wifi settings, your certificate should appear in the CA
cert dropdownbox





file not found, when I typed "file://sdcard/


content.


the


or
and


get on
have
to
when


word
enterprise :
so it


problem here


protected
certificate.
Advanced,
still


WiFi,
how do I
> > > Alex

2. About my posts and membership

Two of my posts here on this forum got somehow copied to
"World_wide_with_god_ministry" forum. I don't know anything about this
group; never joined them, never visited their page.Now, I have them
listed under My Groups drop down. I can't find a way to unsubscribe to
this group.

Did any one else have this problem?

-- 

3. DroidFanz offering unauthorized paid apps - check yours

4. Droid - won't rotate image captured from camera? (Camera.Parameters)

5. Question about sockets and connectivity

6. AudioTrack - Doesn't stop playing as requested.

7. Visual Studio Android Development