4

I stumbled upon the expression ?attr/selectableItemBackground through android - apply selectableItemBackground in xml with support v7.
I would like to look into the exact functionality of that expression, since I do not understand what the question mark at the beginning symbolizes and how exactly it accomplishes its task.

It says that it is part of the support library v7, but I tried looking it up and couldn't find helpful insight into

  • what exactly it is,
  • where it is defined and
  • what precisely it does.
Community
  • 1
  • 1
J0hj0h
  • 894
  • 1
  • 8
  • 34

1 Answers1

8

The syntax ?attr/something means "use the value of the attribute named {something} that was defined for the current theme".

selectableItemBackground is the name of an attribute in your app's theme (usually in styles.xml). You might not be setting a value for it in your theme, but it might have a value in the parent theme that yours extends from, so your theme has that value as well.

This syntax is useful when you might be using the same layout in places that use different themes. For example, suppose you have two themes:

<style name="Theme.Foo" parent="..." >
    <item name="android:textColorPrimary">@android:color/white</item>
    ...
</style>

<style name="Theme.Bar" parent="..." >
    <item name="android:textColorPrimary">@android:color/black</item>
    ...
</style>

And suppose in one of your layout files you have this:

<TextView
    ...
    android:textColor="?android:attr/textColorPrimary" />

Depending on which of these two themes is being used when the layout is inflated (e.g. when you use setContentView()), the TextView could have either white or black text color.

Karakuri
  • 38,365
  • 12
  • 84
  • 104