Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Resource ID

Must the resource ID's for views in XML layouts be unique across all layouts?

For example, I'm working on a little recipe manager app. I have a layout file for adding a new ingredient. In this layout I have an EditText for the ingredient that I'd like to call "edt_name". But I'm afraid that this name is too general; e.g. I might also have an EditText for a recipe name, a cooking procedure name, etc in other XML layout files.

However, I also don't want to make the labels more complex than necessary. I'd like to avoid calling the aforementioned EditText "edt_name_new_ingredient" if I could.

I'm curious as to how developers organize their resources in general. Android doesn't support sub-directories for resources as far as I know, so naming schemes can get really messy.

like image 406
Tianxiang Xiong Avatar asked Aug 04 '11 04:08

Tianxiang Xiong


People also ask

What is the use of resource ID in Android?

drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R. drawable. icon ). This integer is the resource ID that you can use to retrieve your resource.

What is ID value resource Android studio?

Id of a resource is uniq identifier , such as name of a variable. It should be uniq or you will have problems with names.

What is Android ID XML?

For example, when you specify an ID to an XML resource using the plus sign — in the format android:id="@+id/myView" —it means that a new ID resource with the name “myView” needs to be created, and if it does not exist, create a unique integer for it and add to R. java .

What is dimens XML in Android?

xml: The dimens. xml is used for defining the dimensions for different widgets to be included in the Android project.


2 Answers

No, resource ID should not be unique across different xml layouts however they must be unique in a particular xml file.

like image 195
success_anil Avatar answered Oct 17 '22 13:10

success_anil


Resource IDs are namespaced within the package. When you access a resource (in XML, for example), the package name is implicitly set to the current one. You can have other resource files in a different package and refer to those within your code or XML (this is how one accesses the platform resources that come with the SDK).

Similarly in code, you can access a different package's R class and use its resources, but all those within the same package must have unique names.

More info can be found in the documentation here.

like image 42
Eugene S Avatar answered Oct 17 '22 13:10

Eugene S