Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Styles: Difference between 'style="@android:style/XYZ"' and 'style="?android:attr/XYZ"'?

I'm trying to style buttons to look like those I ask about in Android Full Width ICS style Minimalist Bottom ButtonsViews.

I've succeeded, with the following xml for anyone interested:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:divider="@android:drawable/divider_horizontal_dark"
        android:gravity="bottom"
        android:orientation="vertical"
        android:paddingTop="16dip"
        android:showDividers="beginning|end" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:measureWithLargestChild="true"
            android:orientation="horizontal"
            android:divider="@android:drawable/divider_horizontal_dark"
            android:showDividers="middle" >                             


            <Button
                android:id="@+id/cancel_button"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_weight="1"
                android:maxLines="2"
                android:text="@string/cancel_button" />

            <Button
                android:id="@+id/login_button"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="1"
                android:filterTouchesWhenObscured="true"
                android:maxLines="2"
                android:text="@string/login_button" />

        </LinearLayout>
    </LinearLayout>

One question though. The eclipse content assist has no idea what's happening with the following resource resolution:

style="?android:attr/buttonBarButtonStyle"

I'm familiar with the typical resolution (which eclipse's content assist is aware of)

style=@android/style/...

...but I'm not clear on the difference between the two. It seems like some style attributes appear in one but not the other. For instance, the following doesn't resolve to anything:

style=@android:attr/buttonBarStyle

and neither does this:

style="@android:style/buttonBarStyle

So I guess two questions here:

  1. Why the difference in resource referencing syntax?
  2. Why the confusing miscategorization of styles in under an attr category.
  3. What is the attr category even used for again?

Thanks!

like image 934
Teddy Avatar asked Jun 22 '12 14:06

Teddy


People also ask

What is the difference between themes and styles in Android?

A style is a collection of attributes that specify the appearance for a single View . A style can specify attributes such as font color, font size, background color, and much more. A theme is a collection of attributes that's applied to an entire app, activity, or view hierarchy—not just an individual view.

What is attr in Android XML?

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.

Where is style defined in Android?

A style is defined in an XML resource that is separate from the XML that specifies the layout. This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file. The name of the XML file is arbitrary, but it must use the . xml extension.

What is styles XML?

Note: A style is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine style resources with other simple resources in the one XML file, under one <resources> element. The filename is arbitrary.


2 Answers

From another answer on SO (paraphrased here):

Using a question mark in front of an ID means that you want to access a style attribute that's defined in a style theme, rather than hardcoding the attribute.

Think of it as dereferencing a theme attribute to fetch the resource it points to rather than referring to the attribute itself.

Referencing Style Attributes

like image 102
Barak Avatar answered Nov 01 '22 09:11

Barak


?android:attr/buttonBarButtonStyle or just ?android:buttonBarButtonStyle - (attr is optional)

<item name="buttonBarButtonStyle">@android:style/Widget.Button</item>

This internally dereferences to @android:style/Widget.Button which is defined within appcompat/res/values/themes.xml file.

(?android:) - always links to the value of the attribute from the current-theme applied. Thus allowing us to refer the android defined style only rather creating a new one and supplying a hard-coded value.

Google Doc says, "It uses the style that is defined by this attribute, in the current theme"

like image 43
Gagandeep Avatar answered Nov 01 '22 08:11

Gagandeep