What is the easiest way to find out what the value is for a particular attribute in my code using a certain theme in Android Studio? For example, when I see the attribute android:textColorPrimary in my code, I want to know that the resolved value is @color/abc_primary_text_material_light using Theme.AppCompat.Light.
- BTW i just found an easier way, so you may want to check it out.arekolek– arekolek2017-03-10 16:19:23 +00:00Commented Mar 10, 2017 at 16:19
1 Answer
I just found a super easy way to find out the value.
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="?android:textColorPrimary" /> And you want to know what the actual color will be.
You can simply place your cursor at the ?android:textColorPrimary part and click View -> Quick Documentation (or hit F1 or whatever the hotkey you have assigned to that action):
You can go to Design tab and switch parameters like API level, then go back to Text tab, and hitting F1 will show you the value for those new parameters.
If you don't trust Android Studio, you could verify it at runtime:
fun Context.getAttributeValue(attr: Int): String? { return TypedValue() .apply { theme.resolveAttribute(attr, this, true) } .resourceId .takeUnless { it == 0 } ?.let(resources::getResourceName) } println("snackbarStyle: " + getAttributeValue(R.attr.snackbarStyle)) println("datePickerStyle: " + getAttributeValue(android.R.attr.datePickerStyle)) Below is a more winded way to get the same:
For some attributes that are key to your theme, like android:textColorPrimary , it is possible to view and edit them through Tools > Android > Theme Editor.
Other than that, if you are interested in the value of the attribute used by some particular view in your layout, you can use the layout editor to select that view, then in the Properties pane select View all properties, find the one you are interested in and it should show you the default attribute used for this property, for example:
You could then click on the color to get a window to search resources, that also shows you how Android resolves attributes to concrete values:



