What does this mean
#[+][package:]type:name
or this
#+id/my_id
?
What plus does? No detailed explanation in apidoc http://developer.android.com/reference/android/R.attr.html#id
#[+][package:]type:name
You can use the id of an element from other packages. That's how themes and other variable content works. They take resource from other packages.
The plus symbol, +, indicates that this is a new resource ID and the aapt tool will create a new resource integer in the R.java class, if it doesn't already exist.
Refer:
http://developer.android.com/guide/topics/resources/layout-resource.html#idvalue
#+id/my_id does mean u create a new ID for that layout object in xml.
This id is used to mount it in your JavaCode using findViewById() or similar.
For more infos:
http://developer.android.com/guide/topics/resources/layout-resource.html
Related
<WebView android:id="#+id/googleWebview"/>
Here I can set an ID as the string "googleWebview" but if I try to use setId() programatically it expects an integer. Why is this?
In Android all view IDs are integers - #+id/googleWebView is just a label for an integer ID.
In this case the #id/ indicates that it's handling an ID reference, the + means that this is a new ID that should be generated. Under the hood, Android stores these generated IDs in the R file, and you can access reference them programmatically as R.id.{label}.
When the app is build "#+id/googleWebview" is actually turned into an int. It will go to the R class. To access any ids you defined before in your xml files you can simply write R.id.idName in your code so in your case:
view.setId(R.id.googleWebview);
for example
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.
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.
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