Kid24
Kid24

Reputation: 4471

how to get Theme attributes values

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

Upvotes: 35

Views: 32141

Answers (3)

Kid24
Kid24

Reputation: 4471

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

Upvotes: 49

lenooh
lenooh

Reputation: 10682

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

Upvotes: 2

KitKat
KitKat

Reputation: 1515

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.

Upvotes: 2

Related Questions