Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get Theme attributes values

Tags:

android

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(..))

like image 665
Kid24 Avatar asked May 19 '11 00:05

Kid24


People also ask

What attr in Android?

The Attr interface represents an attribute in an Element object. Typically the allowable values for the attribute are defined in a schema associated with the document.

What is ATTR colorSurface?

attr/colorSurface A color for surfaces of components, such as cards, sheets, and menus.


2 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(); 
like image 135
Kid24 Avatar answered Sep 28 '22 03:09

Kid24


JavaDoc:

method TypedArray android.content.res.Resources.Theme.obtainStyledAttributes(int[] attrs)

Return a TypedArray holding the values defined by Theme which are listed in attrs.

Be sure to call TypedArray.recycle() when you are done with the array.

like image 31
KitKat Avatar answered Sep 28 '22 03:09

KitKat