[android-developers] Re: How to read resource based on the current theme
Yes... I was not feeling very comfortable with having to index the items in the array either. For some reason I did not make the connection that R.styleable actually is the array of integers I need. Just to make sure I did it correct this time: In attrs.xml I have: declare-styleable name=PrimaryItemColors attr name=PrimaryColor format=color / attr name=PrimaryReadColor format=color / /declare-styleable The code to read them is: TypedArray atts = context.obtainStyledAttributes (R.styleable.PrimaryItemColors); _PrimaryColor = atts.getColorStateList (R.styleable.PrimaryItemColors_PrimaryColor); _PrimaryReadColor = atts.getColorStateList (R.styleable.PrimaryItemColors_PrimaryReadColor); atts.recycle(); Thank you again for your help. Stefan On Nov 22, 8:39 pm, Dianne Hackborn hack...@android.com wrote: Note: you really really should not be creating these arrays yourself, because the attributes need to be in a specific order for them to work. Use declare-styleable in one of your resource files (usually in attrs.xml where you declare your attributes) to define the sets of resources you want to retrieve. On Sun, Nov 22, 2009 at 1:19 PM, Stefan skyntc...@gmail.com wrote: That was exactly what I needed. Thank you very much for your help. In case somebody has a similar need this is the code that worked for me: R.attr.PrimaryColor, R.attr.PrimaryReadColor are the styled attributes (of type Color State List in my case) that have a specific value in each theme TypedArray atts = context.obtainStyledAttributes(new int[] { R.attr.PrimaryColor, R.attr.PrimaryReadColor}); _primaryColor = atts.getColorStateList(0); _primaryReadColor = atts.getColorStateList(1); Stefan On Nov 22, 2:48 pm, Dianne Hackborn hack...@android.com wrote: Resources does not let you access themes -- it doesn't know about a Theme, since there is one Resources object for you application, but an arbitrary number of themes. Typically to load themed resources, you would do what standard views and widgets do -- declare a styleable array of all of the attributes you want to retrieve, and use Context.obtainStyledAttributes or related to retrieve theme. The LabelView API demo shows an example of this: http://developer.android.com/guide/samples/ApiDemos/src/com/example/a... On Sun, Nov 22, 2009 at 4:22 AM, Stefan skyntc...@gmail.com wrote: In my case I need to be able apply a color conditionally (for example based if an item is read or unread) so I need to be able to read the read and unread colors from the current theme and apply them appropriately. I gave the color special attribute name in my \attr xml file and assigned it different values in 2 different themes. if I do getResources().getColorStateList( my attribute resource ID) it fails because it can't find the resource. It seems like something needs to first resolve the attribute Id to the Id of the resource in the current theme, but I am not sure what does that. Stefan -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.com android-developers%2bunsubscr...@googlegroups.comandroid-developers%252bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- Dianne Hackborn Android framework engineer hack...@android.com 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- Dianne Hackborn Android framework engineer hack...@android.com 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group
[android-developers] Re: How to read resource based on the current theme
That was exactly what I needed. Thank you very much for your help. In case somebody has a similar need this is the code that worked for me: R.attr.PrimaryColor, R.attr.PrimaryReadColor are the styled attributes (of type Color State List in my case) that have a specific value in each theme TypedArray atts = context.obtainStyledAttributes(new int[] { R.attr.PrimaryColor, R.attr.PrimaryReadColor}); _primaryColor = atts.getColorStateList(0); _primaryReadColor = atts.getColorStateList(1); Stefan On Nov 22, 2:48 pm, Dianne Hackborn hack...@android.com wrote: Resources does not let you access themes -- it doesn't know about a Theme, since there is one Resources object for you application, but an arbitrary number of themes. Typically to load themed resources, you would do what standard views and widgets do -- declare a styleable array of all of the attributes you want to retrieve, and use Context.obtainStyledAttributes or related to retrieve theme. The LabelView API demo shows an example of this: http://developer.android.com/guide/samples/ApiDemos/src/com/example/a... On Sun, Nov 22, 2009 at 4:22 AM, Stefan skyntc...@gmail.com wrote: In my case I need to be able apply a color conditionally (for example based if an item is read or unread) so I need to be able to read the read and unread colors from the current theme and apply them appropriately. I gave the color special attribute name in my \attr xml file and assigned it different values in 2 different themes. if I do getResources().getColorStateList( my attribute resource ID) it fails because it can't find the resource. It seems like something needs to first resolve the attribute Id to the Id of the resource in the current theme, but I am not sure what does that. Stefan -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- Dianne Hackborn Android framework engineer hack...@android.com 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: How to read resource based on the current theme
Note: you really really should not be creating these arrays yourself, because the attributes need to be in a specific order for them to work. Use declare-styleable in one of your resource files (usually in attrs.xml where you declare your attributes) to define the sets of resources you want to retrieve. On Sun, Nov 22, 2009 at 1:19 PM, Stefan skyntc...@gmail.com wrote: That was exactly what I needed. Thank you very much for your help. In case somebody has a similar need this is the code that worked for me: R.attr.PrimaryColor, R.attr.PrimaryReadColor are the styled attributes (of type Color State List in my case) that have a specific value in each theme TypedArray atts = context.obtainStyledAttributes(new int[] { R.attr.PrimaryColor, R.attr.PrimaryReadColor}); _primaryColor = atts.getColorStateList(0); _primaryReadColor = atts.getColorStateList(1); Stefan On Nov 22, 2:48 pm, Dianne Hackborn hack...@android.com wrote: Resources does not let you access themes -- it doesn't know about a Theme, since there is one Resources object for you application, but an arbitrary number of themes. Typically to load themed resources, you would do what standard views and widgets do -- declare a styleable array of all of the attributes you want to retrieve, and use Context.obtainStyledAttributes or related to retrieve theme. The LabelView API demo shows an example of this: http://developer.android.com/guide/samples/ApiDemos/src/com/example/a... On Sun, Nov 22, 2009 at 4:22 AM, Stefan skyntc...@gmail.com wrote: In my case I need to be able apply a color conditionally (for example based if an item is read or unread) so I need to be able to read the read and unread colors from the current theme and apply them appropriately. I gave the color special attribute name in my \attr xml file and assigned it different values in 2 different themes. if I do getResources().getColorStateList( my attribute resource ID) it fails because it can't find the resource. It seems like something needs to first resolve the attribute Id to the Id of the resource in the current theme, but I am not sure what does that. Stefan -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.com android-developers%2bunsubscr...@googlegroups.comandroid-developers%252bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- Dianne Hackborn Android framework engineer hack...@android.com 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- Dianne Hackborn Android framework engineer hack...@android.com 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en