Just got started w/Android on my Win 7 box after a hiatus following my first attempt.
In playing with the XML layout I see that the referencing convention defined are of the following forms
#[+][package:]type:name
?[package:][type:]name
I know name is probably the identifier for the resource referenced, and the rest of each string is probably a qualifier E.g. #+id/myTextView
Here, myTextView is a name defined in another file in the same package (I think)
Specifically, what do the symbols '+', and '?' signify?
From Android's documentation:
The at-symbol (#) at the beginning of
the string indicates that the XML
parser should parse and expand the
rest of the ID string and identify it
as an ID resource.
The plus-symbol (+) means that this is
a new resource name that must be
created and added to our resources (in
the R.java file)
just look at http://developer.android.com/guide/topics/ui/declaring-layout.html
Related
Why put # in xml file of android to access some resources?
<android:background="#drawable/coloreffect">
why put before # in xml file of android to access some resources?
That is to distinguish a reference to a resource from other things.
For example, not only can you have android:background="#drawable/coloreffect", but you can have android:background="#ffff0000" to refer to a specific color. Not only can a TextView have android:text="#string/foo", but it can have android:text="Foo", for a hard-coded string. And so on.
It is used to differentiate normal strings and string referred to resources.
For example when you write
android:background="resource name without #"
then it act as static value it does not bind from your resources.
The special character # is used to indicate that the following string will reference a resource file.
If you don't put it then drawable/coloreffect is interpreted as a simple string.
Android follows different syntax for referring to resources, styles etc.
Resources can be referenced from resources using a special XML syntax, such as #drawable/myimage
To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (#), use a question-mark (?), and the resource type portion is optional.
For more details https://developer.android.com/guide/topics/resources/accessing-resources.html
Hope this helps.
Example:
Android complains that ' ' is not a valid resource name character.
Am I forced to use say "New_Delhi" and then programatically map this in my program?
Yes, for the simple reason that Java fields cannot have spaces in their names. A string resource named New_Delhi is referenced in Java as R.string.New_Delhi. A space, in lieu of the underscore, would not be valid Java syntax.
I'm trying to understand how the # symbol in android XML files are parsed.
For example:
<item type="layout" name="activity_item_list">#layout/activity_item_twopane</item>
The #layout references the Layouts in the system.
or
<FrameLayout android:id="#+id/item_detail_container" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_weight="3" />
The #+id seems to via some magic add the ID to the system in a manner that I can reference in code :
R.id.activity_item_detail
My searching is leading me to a number of random articles that don't seem to help my understand how these characters are parsed. are these just treated as some form of macro \ where can I learn a little more about his?
Thanks
Warrick
At http://developer.android.com/guide/topics/ui/declaring-layout.html you can read:
The at-symbol (#) at the beginning of the string indicates that the
XML parser should parse and expand the rest of the ID string and
identify it as an ID resource.
The plus-symbol (+) means that this is
a new resource name that must be created and added to our resources
(in the R.java file).
There are a number of other ID resources that
are offered by the Android framework. When referencing an Android
resource ID, you do not need the plus-symbol, but must add the android
package namespace, like so: android:id="#android:id/empty"
Does it help?
#layout, #string, etc. are for accessing resources, see http://developer.android.com/guide/topics/resources/accessing-resources.html
#+id is id declarations, you can use the id below the declaration in the xml file, e.g. in layout_toLeftOf property, and access the UI element in code from R.id package
official link here
They say:
The at sign (#) is required when you're referring to any resource object from XML. It is followed by the resource type (id in this case), a slash, then the resource name (edit_message).
I'm not quite understand what # mean, who can give a more specific explanation?
If the ID is not already created, then + will create it. You will need it only the first time you refer to that id.
From docs :
The plus sign (+) before the resource type is needed only when you're
defining a resource ID for the first time. When you compile the app,
the SDK tools use the ID name to create a new resource ID in your
project's gen/R.java file that refers to the EditText element. With
the resource ID declared once this way, other references to the ID do
not need the plus sign.
Like the docs say, the # symbol means you are referring to an xml resource.
For example : #string/edit_message means you are trying to refer to a string with the id edit_message defined in an xml file.
# sign means you are referring to a resource under res folder.
#drawable
refer to a .png or button state images.
#anim
refer to animation
#id/anyID
refer to any pre Created id
#+id/createNewID
+ create a new id and name it createNewID and
+sign only works with id. you cannot use it with anim,drawable.
Note
You can not create any Resource programatically other than id.
# is used to give identity or to refer to the existing resource in XML File.
'#' specifies what kind of resource you are about to access.
if you use #id, it will refer to any resources in layout of project.
if you use #string, it will locate to a string that you may usually specify in Strings.XML
there are so many other resources like :
#drawable, #color, #style, #array, #dimen, #layout & #xml..
you can use according to your requirement.
In each android project there is a gen folder inside src where R.java is created automatically and all the IDs of resources used/created in XMLs belong here.
As the docs say:
The at sign (#) is required when you're referring to any resource object from XML.
This means that you are going to refer an ID from R.java if you add + then the corresponding id will be created if it does not exists in R.java already. As the docs say:
The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project's gen/R.java file that refers to the EditText element. With the resource ID declared once this way, other references to the ID do not need the plus sign.
So + sign is required only when you want to create a new id for the view. If it is already created in the same file then it will simply reuse that id instead of creating a new id with the same name.
NOTE: you can use + with IDs only.
# has so many uses. The one which are used mostly are:
#id/ to reference from R.id
#string/ references R.string similarly...
#drawable/ -> R.drawable
#array/ -> R.array
#color/ -> R.color
Finally you can use #android to refer resources defined in the android SDK.
When we add some entries in the strings.xml file or layout.xml file, then the R.java file gets modified automatically. Again, if we want to refer something from the layout file such as reading the EditText value entered by the user, then again we refer the R.java file at our java code to read the values.
What is this R.java file all about? The values of each entry of this R.java file seems to be in HEXADECIMAL format but what is its use?
I have read the doc but i get fairly confused for this R.java :(
Please someone step forward and explain what is this R.java file all about :(
Regards,
http://developer.android.com/guide/topics/ui/declaring-layout.html says:
android:id="#+id/my_button"
The at-symbol (#) at the beginning of the string indicates that the
XML parser should parse and expand the rest of the ID string and
identify it as an ID resource. The plus-symbol (+) means that this is
a new resource name that must be created and added to our resources
(in the R.java file).
The R.java file is generated by the Android Resource Manager (aapt.exe) and contains references to all resources of your app. Each reference is a unique id (public static final int). These constants are written to the R.java file in hexadecimal format. The logic of assigning specific integer to each resource is private to the Android Resource Manager. You can look at the source code of aapt.exe on the internet, e.g. at http://gitorious.org/rowboat/frameworks-base/trees/d58fb97ddf052b3ceac921ac7e936af990392b2c/tools/aapt
It turns resource objects into Java recognizable names for you to refer to in your code.
The R class is basically the way Android provide resource access from within your code. As you wrote when you alters strings.xml etc on save of that resource file Android SDK will recompile the R class to make your changes accessible for within your code. What the values in R class are is more or less not important since its used by Android internally to map, for example a string to an ID.
To reference a string from within your code you use R like this:
R.string.MyString
If you string in string.xml is called MyString. Same for layouts etc.
I guess this is what you read, but otherwise it's pretty good explained here.