Is it possible to obtain styled attributes values from particular Theme without setting the theme up to application/activity? (I mean before invoking context.setTheme(..))
3 Answers
For example, to get editTextColor attribute's value of a theme called MyTheme:
TypedArray a = getTheme().obtainStyledAttributes( R.style.MyTheme, new int[] { R.attr.editTextColor }); // Get color hex code (eg, #fff) int intColor = a.getColor(0 /* index */, 0 /* defaultVal */); String hexColor = Integer.toHexString(intColor); // Don't forget to recycle a.recycle(); 2 Comments
HGPB
...and if you are looking for a way to also get the themes style resource id dynamically see here: stackoverflow.com/a/9537629/317889
ayke
You should call
a.recycle() when you're done with the TypedArray.JavaDoc:
method
TypedArrayandroid.content.res.Resources.Theme.obtainStyledAttributes(int[]attrs)Return a
TypedArrayholding the values defined by Theme which are listed in attrs.Be sure to call
TypedArray.recycle()when you are done with the array.
1 Comment
Sarah Multitasker
What happens, if not? E.g. because I leave the function scope?
if you need it in the xml file, you can use something like this:
style="?android:attr/panelTextAppearance" for example:
<TextView style="?android:attr/panelTextAppearance" android:paddingTop="?android:attr/paddingTop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/your_text" /> if you're using eclipse, control+click on the item, to see other possible values (a file attrs.xml will open).
a.recycle()! Reference: stackoverflow.com/questions/7252839/…