is there any setStyle method?

4847 views
Skip to first unread message

skink

unread,
Feb 1, 2009, 5:28:43 PM2/1/09
to Android Developers
hi,

i created my style in styles.xml, lets say MyStyle.

i can use it in any layout file by style="@style/MyStyle".

also my generated R class have R.style.MyStyle, my question is if i
can use it at runtime to "setStyle" - unfortunately View doesn't have
such a method. if not what is R.style.MyStyle used for? (i already
know that custom themes IDs are also in R.style and can be used to
setTheme, but MyStyle is not a theme)

Romain Guy

unread,
Feb 1, 2009, 6:34:23 PM2/1/09
to android-d...@googlegroups.com

The style can be passed to a view's constructor.

skink

unread,
Feb 1, 2009, 7:30:22 PM2/1/09
to Android Developers


On 1 Lut, 22:34, Romain Guy <romain...@google.com> wrote:
> The style can be passed to a view's constructor.
>


thanks Romain for your reply, but i think it's equivalent of setting
style in xml - it's static.

what about setting the style after the View is constructed - that way
style could be dynamically changed?

Dianne Hackborn

unread,
Feb 1, 2009, 9:36:56 PM2/1/09
to android-d...@googlegroups.com
Sorry there is no way to change the style/theme that a view is using after is is created.
--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support.  All such questions should be posted on public forums, where I and others can see and answer them.

Paranoia

unread,
Feb 2, 2009, 4:58:12 AM2/2/09
to Android Developers
so android does not support dynamic theme, right?
do you guys have any plan to support it?

On Feb 2, 10:36 am, Dianne Hackborn <hack...@android.com> wrote:
> Sorry there is no way to change the style/theme that a view is using after
> is is created.
>
>
>
> On Sun, Feb 1, 2009 at 4:30 PM, skink <psk...@gmail.com> wrote:
>
> > On 1 Lut, 22:34, Romain Guy <romain...@google.com> wrote:
> > > The style can be passed to a view's constructor.
>
> > thanks Romain for your reply, but i think it's equivalent of setting
> > style in xml - it's static.
>
> > what about setting the style after the View is constructed - that way
> > style could be dynamically changed?
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com

skink

unread,
Feb 2, 2009, 5:22:15 AM2/2/09
to Android Developers


Dianne Hackborn napisał(a):
> Sorry there is no way to change the style/theme that a view is using after
> is is created.

thank you Dianne,

but what about dynamic setStyle in the future releases? imagine i have
a label (TextViev) that normally in plain/white/15dip but when
something is wrong it's bold/orange/18dip. instead of using three
setters i could use label.setStyle(R.style.Label_Normal)
or label.setStyle(R.style.Label_Warning)

pskink












Romain Guy

unread,
Feb 2, 2009, 11:36:39 AM2/2/09
to android-d...@googlegroups.com

Textview supports setTextAppearance if I remember correctly.

--~--~---------~--~----~------------~-------~--~----~ You received this message because...

Dianne Hackborn

unread,
Feb 2, 2009, 1:00:49 PM2/2/09
to android-d...@googlegroups.com
Well you'll need to define dynamic theme.  What specifically are you asking about?
hac...@android.com

skink

unread,
Feb 2, 2009, 2:14:30 PM2/2/09
to Android Developers


Romain Guy wrote:
> Textview supports setTextAppearance if I remember correctly.

indeed ! Textview supports public void setTextAppearance(Context
context, int resid)

so its even more mystery that there is no generic View.setStyle(int
resid) method....

for example:
suppose i want also change some padding, in this case
setTextAppearance is not enough...


Romain Guy

unread,
Feb 2, 2009, 2:17:08 PM2/2/09
to android-d...@googlegroups.com
Supporting dynamic style change is much more complicated than just
changing the text appearance. It is not supported at the moment,
that's all.
--
Romain Guy
Android framework engineer
roma...@android.com

Kennard Consulting

unread,
Feb 4, 2009, 9:37:32 PM2/4/09
to Android Developers
Romain,

Dianne pointed me to your post. When you say:

> The style can be passed to a view's constructor.

Note the poster says he has an R.style.MyStyle. So which constructor
accepts an id? I only see ones that accept AttributeSets. Is there a
way to create an AttributeSet from an id?

Regards,

Richard.

Dianne Hackborn

unread,
Feb 4, 2009, 9:48:39 PM2/4/09
to android-d...@googlegroups.com
I believe you can pass in null for it.
--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support.  All such questions should be posted on public forums, where I and others can see and answer them.

Kennard Consulting

unread,
Feb 5, 2009, 4:49:04 AM2/5/09
to Android Developers
Dianne,

> I believe you can pass in null for it.

No. That doesn't seem to work. If I do 'new TextView( context, null,
myStyleId )' the label appears but the style is not applied. If I do
'new EditView( context, null, myStyleId )' the textbox doesn't appear
at all (maybe it has zero width?).

The documentation seems to suggest the 'new View( context,
AttributeSet, defStyle )' constructor is intended for subclasses, and
doesn't really go into how to call it externally. So I think I do need
to pass it an AttributeSet. The question is, where can I get such an
AttributeSet?

Richard.

Kennard Consulting

unread,
Feb 9, 2009, 1:34:13 AM2/9/09
to Android Developers
Dianne,

I tried going down the 'declare-styleable' route as you suggested. I'm
a bit stuck because 'declare-styleable' needs to be placed in a
'attrs.xml' file, which gets compiled to R.java in the package
specified by the 'package' attribute in AndroidManifest.xml.

But I am creating a View for other developers to use. So I can't know
in advance what their package will be. So 'declare-styleable' doesn't
seem appropriate? Or am I missing something?

Please help! Is it possible in R2 to programmatically apply styles?

Regards,

Richard.

P.S. With regard to AttributeSet, I found this thread
http://markmail.org/message/ogskv4frewsxghlp which suggests it is a
known issue for now?
Reply all
Reply to author
Forward
0 new messages