Paul A Jungwirth Paul A Jungwirth - 3 months ago 10
Android Question

Refer to color in built-in theme in my layout file

My Android application is using the Holo Light theme. In my layout I want to make sure I use the same color as the rest of the theme, like this:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/hint_foreground_light"
android:text="@string/welcome_city"/>


I can see that that color is defined here:

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml#505

I've also tried
@android:color/hint_foreground_light
and
?textColorHint
, but I get errors for all of them. How do I use the colors from a built-in theme?

I'm seeing errors like:

No resource found that matches the given name (at 'textColor' with value '@color/hint_foreground_light').
Resource is not public. (at 'textColor' with value '@android:color/hint_foreground_light').
No resource found that matches the given name (at 'textColor' with value '?textColorHint').


(The reason I'm even messing with setting
android:textColor
is because this
TextView
is for a
Spinner
and is showing up as nearly-white-on-white by default.)

Answer

The color value you referenced is indeed private and is not meant to be accessed. However, you can access it using:

android:textColor="@*android:color/hint_foreground_light"

The IDE will show an error but since, its xml, it'll compile and will also work flawlessly. This method is not recommended as you never know when will those value change or become inaccessible.

It is recommended that you simply copy the required resource in your project and reference it from there.

Comments