How to get an ?attr/ value programmatically

Viewed 8215

I'm trying to do some custom view styling and I'm having trouble correctly picking up styled attributes from the theme.

For instance, I would like to get the themes EditText's Text Colour.

Looking through the theme stack you can see my theme uses this to style it's EditText's:

<style name="Base.V7.Widget.AppCompat.EditText" parent="android:Widget.EditText">
    <item name="android:background">?attr/editTextBackground</item>
    <item name="android:textColor">?attr/editTextColor</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
</style>

What I'm looking for, is how do I get that ?attr/editTextColor

(Aka, the value assigned by the theme to "android:editTextColor")

Searching through google, I have found enough of this answer:

TypedArray a = mView.getContext().getTheme().obtainStyledAttributes(R.style.editTextStyle, new int[] {R.attr.editTextColor});
int color = a.getResourceId(0, 0);
a.recycle();

But I'm pretty sure I must be doing this wrong as it always displays as black rather than grey?

2 Answers
Related