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.
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.
Id of a resource is uniq identifier , such as name of a variable. It should be uniq or you will have problems with names.
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 .
xml: The dimens. xml is used for defining the dimensions for different widgets to be included in the Android project.
No, resource ID should not be unique across different xml layouts however they must be unique in a particular xml file.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With