try using this <attr name="myColor" format="reference" />
On Wed, Sep 22, 2010 at 9:26 AM, Mariano Kamp <[email protected]> wrote: > As it wasn't possible to submit a new post to the existing/old conversation > using the web interface I am copying the message here with Danilo's > permission: > Hi Mariano. > > Did you manage to work using themes from drawables xml? > I have same problem and there is no any reference but this thread and > there is no solution here. > If you find solution please let me know. > > I currently "solved" it by using different drawable xml for every > theme but as you can see this isn't a god solution at all. > > About your question about getting a color from a theme (I know its old > but for future readers :) ) solution I created is like this: > > private static int getColor(final Context context, final int id, > final int defaultColor) { > try { > final Theme theme = context.getTheme(); > if (theme == null) { > return defaultColor; > } > TypedValue typedValue = new TypedValue(); > > if (theme.resolveAttribute(id, typedValue, false)) { > final Resources resources = > context.getResources(); > final int resourceId = typedValue.resourceId; > if (typedValue.type == > TypedValue.TYPE_INT_COLOR_RGB8 || > typedValue.type == TypedValue.TYPE_INT_COLOR_RGB4 || typedValue.type > == TypedValue.TYPE_INT_COLOR_ARGB4 > || typedValue.type == > TypedValue.TYPE_INT_COLOR_ARGB8 || > typedValue.type == TypedValue.TYPE_FIRST_COLOR_INT) { > return typedValue.data; > } else { > return > resources.getColor(resourceId); > } > } > return defaultColor; > } catch (NotFoundException e) { > // TODO Auto-generated catch block > e.printStackTrace(); > return defaultColor; > } > } > > On Tue, May 11, 2010 at 9:08 AM, Mariano Kamp <[email protected]> > wrote: >> >> Hi Dianne. >> Thanks very much, almost there. >> I understand the procedure, it would do what I want, but I run into an >> exception: >> D/NewsRobDefaultExceptionHandler( 1839): Caused by: >> java.lang.UnsupportedOperationException: Can't convert to color: type=0x2 >> D/NewsRobDefaultExceptionHandler( 1839): at >> android.content.res.TypedArray.getColor(TypedArray.java:273) >> D/NewsRobDefaultExceptionHandler( 1839): at >> android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:647) >> D/NewsRobDefaultExceptionHandler( 1839): at >> android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788) >> D/NewsRobDefaultExceptionHandler( 1839): at >> android.graphics.drawable.Drawable.createFromXml(Drawable.java:729) >> D/NewsRobDefaultExceptionHandler( 1839): at >> android.content.res.Resources.loadDrawable(Resources.java:1690) >> This happens when using it the way we described here in a drawable xml. >> However to better understand the error I used it on other attributes and >> when using it on the background attribute of a View it works. Maybe this >> approach is incompatible with drawables that are expressed in xml? >> Btw. Is there any way to get the actual #rgb string for a color from a >> theme? >> Cheers, >> Mariano >> On Tue, May 11, 2010 at 8:02 AM, Dianne Hackborn <[email protected]> >> wrote: >>> >>> You need to define an attribute for your value: >>> <attr name="myColor" format="color" /> >>> Then make a custom theme that supplies a value for it: >>> <style name="MyTheme" parent="android:Theme"> >>> <item name="com.my.package:myColor">#ff808080</item> >>> </style> >>> And now you can reference that value from other XML: >>> >>> <shape xmlns:android="http://schemas.android.com/apk/res/android >>> " >>> android:shape="rectangle"> >>> <gradient android:startColor="?com.my.package:myColor" >>> >>> android:endColor="#66FFFFFF" >>> android:angle="270" /> >>> </shape> >>> >>> Note that "com.my.package" is your manifest's package name. >>> Also I just wrote this by hand so I won't guarantee it is completely >>> right. :} >>> On Mon, May 10, 2010 at 11:02 AM, Mariano Kamp <[email protected]> >>> wrote: >>>> >>>> Hmmh, also no answer to this question on themes. Maybe the answer cannot >>>> be given with two lines? So let me explain what I would expect step by step >>>> and you stop me were I deviate from the Golden Path? I'll try yes/no >>>> questions. >>>> In the simplest terms I would expect that in every place I can specify a >>>> color in RGB (#ffff) I could also specify a symbolic name instead. This >>>> should work like @color/xyz, but with one more level of indirection that >>>> lets me switch between themes, like dark and light. >>>> Is that possible? >>>> I would expect those themes to be applicable to (a) widgets, (b) >>>> drawables (see the mentioned example) and (c) when I need to style >>>> something >>>> myself like HTML. Are (a) - (c) possible? >>>> Furthermore I would expect that I could define style keys myself, but I >>>> got the impression that only keys from the "android" namespace can be used? >>>> That lead me to believe that you can't define abstract colors, but only >>>> styles for specific attributes used in the widgets' implementations. >>>> And again, if there is any meaningful documentation I would be happy to >>>> read it. Just send me the link or name of the book. >>>> >>>> On Sat, May 8, 2010 at 3:26 PM, Mariano Kamp <[email protected]> >>>> wrote: >>>>> >>>>> Hi, >>>>> I have trouble (example) wrapping my head around styles/themes. Can >>>>> somebody help me or point me to actual documentation? >>>>> Let's say I have a drawable that I want to use as the background of a >>>>> layout: >>>>> >>>>> <shape xmlns:android="http://schemas.android.com/apk/res/android" >>>>> android:shape="rectangle"> >>>>> <gradient android:startColor="#9F9FA4" >>>>> android:endColor="#66FFFFFF" >>>>> android:angle="270" /> >>>>> </shape> >>>>> >>>>> Now I want to have a dark and a light theme. Depending on the chosen >>>>> theme I want to have a different value for startColor and endColor. How >>>>> do I >>>>> do that? I am aware of extending themes, but what do I put into the items? >>>>> How do I reference those new values then? >>>>> >>>>> Cheers, >>>>> >>>>> Mariano >>>> >>>> -- >>>> 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 >>> >>> >>> -- >>> Dianne Hackborn >>> Android framework engineer >>> [email protected] >>> >>> Note: please don't send private questions to me, as I don't have time to >>> provide private support, and so won't reply to such e-mails. All such >>> questions should be posted on public forums, where I and others can see and >>> answer them. >>> >>> -- >>> 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 > > -- > 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 -- 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

