Craig wrote:
> It well could be over complicated - here is what I did:
> In res, values, attrs.xml I have:
>
> <?xml version="1.0" encoding="utf-8"?>
> <resources>
>       <declare-styleable name="MyColors">
>               <attr name="inactiveColor" format="color"/>
>               <attr name="activeColor" format="color"/>
>                 ...
>
> In res, values, styles.xml:
> <?xml version="1.0" encoding="utf-8"?>
> <resources>
>       <style name="Theme" parent="android:Theme.NoTitleBar"></style>
>
>       <style name="Theme.GreenWithYellow">
>               <item name="android:windowBackground">@drawable/mid_green</item>
>               <item name="inactiveColor">#090</item>
>               <item name="activeColor">#0F0</item>
>                 ...
>
> ...and I specify this theme in my AndroidManifest.xml.
>
>
> Without specifying the attributes separately, I was getting errors for
> my styles.xml like 'No resource found for specified attribute' (not
> the exact wording).
>
> Is there a simpler way to do this?

for xml files, you did perfect job: it's exactly what it should be.

but your code is FAR overcomplicated. look at this:

Theme t = getTheme();
TypedArray myColors = t.obtainStyledAttributes(R.styleable.MyColors);
Log.d("test ", "has activeColor? " + myColors.hasValue
(R.styleable.MyColors_activeColor));
Log.d("test ", "has inactiveColor? " + myColors.hasValue
(R.styleable.MyColors_inactiveColor));
Log.d("test ", "has disabledColor? " + myColors.hasValue
(R.styleable.MyColors_disabledColor));

int activeColor = myColors.getInteger
(R.styleable.MyColors_activeColor, 0);
int inactiveColor = myColors.getInteger
(R.styleable.MyColors_inactiveColor, 0);
Log.d("test ", "activeColor " + Integer.toHexString(activeColor));
Log.d("test ", "inactiveColor " + Integer.toHexString(inactiveColor));

i declared additional attribute (but not defined in themes.xml)
disabledColor to show how to test if given attribute exists in theme
or not (its somehow helpful for colors since its difficult to set
default value in getColor)

and the code outputs:

D/test    (  345): has activeColor? true
D/test    (  345): has inactiveColor? true
D/test    (  345): has disabledColor? false
D/test    (  345): activeColor ff00ff00
D/test    (  345): inactiveColor ff009900

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to