koayst
koayst

Reputation: 2125

When to use "?android" or "@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.

Upvotes: 11

Views: 3020

Answers (6)

Prateek Joshi
Prateek Joshi

Reputation: 4067

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.

Upvotes: 3

peacepassion
peacepassion

Reputation: 1978

I checked a lot on this question for it confuse me a lot once.

The name after ? is you are referencing a Variable name that is defined in the <declare-styleable>.

The name after @ is the style name defined by you or system.

Let me put an analogy by C++.

Say, we have string str1 = "Hello world".

? is like string str2 = str1. It means you are referencing the value of str1.

@ is like string str2 = "Hello world". It means you are using the value directly.

Upvotes: 1

Amit Yadav
Amit Yadav

Reputation: 35074

I am not sure for in short and simple.

? represents that resource definition is a part of a Framework

@ represents that resource definition is a part of a Your Project/Support Library

Upvotes: 1

Ehtesham
Ehtesham

Reputation: 35

@ is used to access defined resources

"@android:drawables/test.png"

? is used to access defined theme style

"?android/attr/style"

Hope this helps

Upvotes: 0

rf43
rf43

Reputation: 4405

AFAIK...

"?[package:][type:]name" refers to an Android style. As an example a progress dialog style attribute would look something like:

style="?android:attr/progressBarStyleLarge"

An explanation

  • ? = ?
  • [package:] = android:
  • [type:] = attr (attribute)
  • name = progressBarStyleLarge (or in your case: starStyle)

However, styles are also referenced using the @...

"@[+][package:]type:name" is a resource of some sort.

So for a style, you would declare all your styles in a file named 'styles.xml" in your 'values' directory.

<style name="mono_text_headers">
    <item name="android:textSize">16sp</item>
    <item name="android:typeface">monospace</item>
    <item name="android:layout_marginBottom">5dp</item>
</style>

Then reference them just like a string, drawable, etc.. style="@style/mono_text_headers"

http://developer.android.com/guide/topics/ui/themes.html

Upvotes: 2

goto10
goto10

Reputation: 4370

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.

Upvotes: 8

Related Questions