Can someone explain the attr? Can someone explain the attr? android android

Can someone explain the attr?


The ?attr/menuIconCamera value means that an icon from menuIconCamera attribute of the current theme will be used.

There must be a drawable assigned to the menuIconCamera attribute somewhere in the themes.xml file. If there're two themes with different values of this attribute then actual icon will depend on a theme which is currently used.

The attrs.xml file is used to define custom attributes. Without this definition compiler will treat unknown attributes as erroneous.


The ?attr: syntax is used for accessing attributes of current theme. See referencing style attributes.


I know this post is very old, but I feel the following explanation will help beginners understand it easily.

So in layman's terms,

someAttribute="?attr/attributeName" means -

set the value of someAttribute to whatever is the value of attributeName in current theme

A common example occurs in styling a Toolbar

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">        <item name="colorPrimary">@color/primary_color</item>       //some more stuff here</style><!-- custom toolbar style --><style name="myToolbar" parent="Widget.AppCompat.Toolbar">      <item name="android:background">?attr/colorPrimary</item>     //some code here</style>

Here value of android:background will be set to @color/primary_color because ?attr/colorPrimary refers to @color/primary_color in the current theme (AppTheme)