Attr

The name part in the resource reference @[package:]type/name is the name given to the resource; it also gets represented as an int constant in R.java. Now we have come to the important part of this syntax: the package. If you don't specify any package, then the pair type/ name will be resolved based on local resources and the application's local R.java package.

If you specify android:type/name, on the other hand, the reference will look in the package identified by android: the android.R.java file, to be precise. So you can use any Java package name in place of the package placeholder to locate the right R.java file to resolve the reference. Based on this information, let's analyze a few examples:

<TextView id="text">

// Compile error, as id will not take raw text strings <TextView id="@text">

// wrong syntax. It is missing a type name

// you will get an error "No Resource type specified

<TextView id="@id/text">

//Error: No Resource found that matches id "text" //Unless you have taken care to define "text" before

<TextView id="@android:id/text"> // Error: Resource is not public

// indicating that there is no such id in android.R.id

// Of course this would be valid if Android R.java were to define

//Success: Creates an id called "text" in the local package

0 0

Post a comment