why we put # before using resources in android xml file? - android

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.

Related

Is there a way to have a Resource name inside strings.xml with a space?

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.

Understanding the Android XML # prefix

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

Android one values folder for multiple languages

Is there a way to tag a strings resource folder with more than one language(values-en-es)?
My problem is that for Hebrew on some devices the language code "iw" and on others it is "he".
My current solution is to make two folders with the same content and only change their name
respectively.
I wonder if there is a more accurate way to do it?
Resource folder names can have multiple qualifiers but only one qualifier per type:
For example
values-en-rGB //Language + Region
is valid but
values-en-fr//Language + Language
is not valid, since it has multiple values for a single qualifier. So
values-iw-he
is not possible.
Source: Android Developers, Qualifier Name Rules.
However this doesn't mean you have to duplicate the files. Instead, you can create an Alias Resource.
Android Developers explains Alias Resouces like this:
Creating Alias Resources: When you have a resource that you'd like to use for more than one device configuration (but do not want to provide as a default resource), you do not need to put the same resource in more than one alternative resource directory. Instead, you can (in some cases) create an alternative resource that acts as an alias for a resource saved in your default resource directory.
For example, a String resource in one folder
<string name="app_name">My Awesome App</string>
can be referenced in another String resource in another folder as:
<string name="application_name">#string/app_name</string>
More about alias-resources on Android Developers.
You can make a File Link in eclipse, as described here.
So you have your values-iw/strings.xml with the real values and you make a File Link to that file in your values-he folder. This has the benefit that you do not have to edit 2 files, the linked 'file' gets updated automatically.

Android R.java use

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.

What do the symbols '+', and '?' when referenced from resource definition signify?

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

Categories

Resources