Home » Android » android – how to get Theme attributes values

android – how to get Theme attributes values

Posted by: admin April 23, 2020 Leave a comment

Questions:

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

How to&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();

Answer:

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.

Answer:

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