Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain the attr?

I am looking at the Honeycomb Gallery sample code (here) and I ran across the following code while trying to add action items in my own app:

<item android:id="@+id/camera"     android:title="Camera"     android:icon="?attr/menuIconCamera"     android:showAsAction="ifRoom" /> 

The ?attr is throwing me for a loop. Can someone please explain what this is doing? How is this related to a drawable? I can't seem to find any good information on Google. Also is there a listing or gallery of attributes we can use for icons instead of just menuIconCamera?

Thanks

Edit: I did some more looking around and found that attrs.xml looks like this:

<resources> <declare-styleable name="AppTheme">     <attr name="listDragShadowBackground" format="reference" />     <attr name="menuIconCamera" format="reference" />     <attr name="menuIconToggle" format="reference" />     <attr name="menuIconShare" format="reference" /> </declare-styleable> 

Unfortunately that just makes it even more confusing for me. What is this doing?

like image 280
FuegoFingers Avatar asked Sep 21 '11 18:09

FuegoFingers


People also ask

How do you use ATTR?

The attr() method sets or returns attributes and values of the selected elements. When this method is used to return the attribute value, it returns the value of the FIRST matched element. When this method is used to set attribute values, it sets one or more attribute/value pairs for the set of matched elements.

What attr in Android?

The Attr interface represents an attribute in an Element object. Typically the allowable values for the attribute are defined in a schema associated with the document.


1 Answers

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.

like image 100
Michael Avatar answered Sep 19 '22 13:09

Michael