Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use "?android" or "@android"?

Tags:

android

I am a newbie and trying to understand the following XML code:

Looking at the documentation at developer.android.com, it says "starStyle" is a constant in R.attr and


public static final int starStyle Since: API Level 1

Default Star style.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

It seems to say that there are 2 syntax I can declare. 1) "@[+][package:]type:name" 2) "?[package:][type:]name".

If there are 2 syntax, what is the correct one for "@[+][package:]type:name" ?

I tried ""@android:attr/starStyle" but I didn't get a "star" checkbox even though the application compiled ok.

like image 246
koayst Avatar asked Oct 16 '11 02:10

koayst


2 Answers

The @-syntax is used for resources that are defined in your project or the Android framework. The ?-syntax is used for resources in the current theme.

For starStyle, which should be defined in the theme, you want:

"?android:attr/starStyle"

See here for some elaboration on theme resources.

like image 174
goto10 Avatar answered Sep 16 '22 14:09

goto10


Android provides a shortcut to enable you to

use styles from the currently applied theme.

To do this, use ?android: rather than @ as a prefix to the resource you want to use.

example:

<EditText
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@android:string/url"
android:textColor="?android:textColor"
/>

This technique enables you to create styles that change if the current theme changes, without you modifying each individual style resource.

like image 38
Prateek Joshi Avatar answered Sep 16 '22 14:09

Prateek Joshi