How R.java file manage ID name declaed in XML layouts? - android

I was searching for difference in #id/ and #+id/ in our XML layouts. I got another question in my mind, Should we use different name for every component declared in layout for our application ?
I know that we can not declare two components with same name in a layout,
But if i talk about entire application. I use same name in other layouts.
Is this the correct way of coding ?
This answer quote that when we declare #+id/ there is new entry in R.java file, If i add same name component in other layout, Will it create other entry in R.java file ???
I am confused in this, and what is the right way of declaring names in XML layout.
Any help would be appreciated.

If you use #+id notation in multiple places, it will not create a new ID value each time it's used. The compiler will reuse the same ID for each of the same name in the same app.
You can use the #id notation when you know you are referencing an existing id. This is common when using relative layouts where you want to align some view with another view of some id in the same layout.

You should use the different name for each view in your application. And for difference for #id and #+id read the content in the following link you understand better Difference between "#id/" and "#+id/" in Android

Related

CheckedTextView Attributes ID and checkMark

Just start developing with android and think instead of reading a book a webinar could be better because a webinar could also teach me short ways and how an android developer thinks when writing the code but now got a problem
<CheckedTextView
android:id="#android:id/text1"
android:checkMark="?android:attr/listChoiseIndicatorMultiple"
</>
I dont understand the above code up to now see lots of different id definitions some of them was for resources and start with #resource/name, and some of those id definitions was like #+id/name just for creating a new id for the component but this time it is using android:id/text1 and I dont understand why it is using it in that manner
Besides, the checkMark thing make me confuse more what are all those ?android:attr/listChoiseIndicatorMultiple means?
Could you please explain me and show me some resource where can I find all those magic attributes so I can cope next time by myself and hope someday can answer other newbie questions
Thanks a lot in advance, and all comment will be appreciated.
Well, reading the docs has always been helpful to me:
Android Developer Site
XML Layout specific docs
#android:id/text1 is just a format used when the id has been previously defined. When you put a + in there that means the framework should create the resource id if it doesn't already exist.
It's normal to use #+id/thisid when defining a new view in a layout, and then use #id/thisid to reference the aforementioned view from another part of the layout (say, in a RelativeLayout where you need to tell one widget to be below another).
A question mark before the ID indicates that you want to access a style attribute that's defined in a style theme, rather than hard-coding the attribute.
#android:id/text1 basically this is used when you create any android component like button, layout, textviews etc.
but when you need any external component which is general for different platform like any color, image etc then you can declare it as #resource/name.
actually there is nothing different just keep one thing in mind that in #android:id/text1, id will simply work as an class name will contains other objects like textview, imageview or any other.
now if you declare #resource/name then in that also instead of id class name will be resource. actually when you will use it in java then these(#android:id/text1) will be converted into object hierarchy.

Any way to organize android 's id "#+id/"

I am wondering is there a way to organize my widget's android:id . My app has a couple of Activies and couple of layout. It is hard to keep track of all the names of buttons and textviews. My IDE would spring up a list of all the R.id.xxx from previous layouts . Is there a way to sort them like with directory or periods, ie android:id="#+id/abc.efg" or android:id="#+id/abc/efg" . Sort of like sub structuring them or nesting them.
A simple way I keep track is by changing the "id" prefix to something else
ex.
A layout for ActivityOne might have layout IDs as
android:id="#+activity1/textview"
And "TestActivity" could be
android:id="#+test/textview"
I am trying to always use some convention on the id naming. For example use type-of-component prefixes: *btn_somethig* for all Buttons, *et_something* for all EditText and so on... When you're looking for a particular ID, just fill-in first the type of the component.
AFAIK no. I always go by a naming convention based on what I'm looking for. Usually it's, type_of_id_type_of_object_name. So a layout could be layout_relative_layout_main_panel. A sub-view like a TextView would be view_text_view_text1 or something. The detail is app-specific though.

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.
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.

Android: can one alias a layout?

I have two Activities that both list items in a table. For the time being both currently use the same layout and then inflate it at runtime.
However, to cleanly and strictly separate the two and also allow me to later maybe have slightly different layouts for each, I would like to have two different layout names but simply alias (is that a verb?) one layout to point to the other.
Can that be done? I tried to define an alias id in the strings.xml file but that only yields errors.
Michael
There is this more straight forward layout aliasing technique as well. You can refer to: https://developer.android.com/training/multiscreen/screensizes.html#TaskUseAliasFilters
What if you have one of them simply contain nothing but a single <include> element so that it just includes the other one?
http://developer.android.com/resources/articles/layout-tricks-merge.html

Is android:id specific to xml file or the whole project?

Is the android:id="#+id/somevalue" specific to the xml file in which it is defined, or the whole android project?
If it is project-wide, it seems like I have a lot of id's to come up for text fields. For example I have to name my "title" field like "title1" and "title2" etc...
They are unique on the whole project but can be used on different contexts. The reason is fairly simple: there will be just one R.id.name_of_id variable, though, they cannot be reference from anywhere. I mean, if you have and ID called #+id/my_id which is on the my_layout.xml file, you cannot use it unless you are currently working with my_layout.xml implicitly or explicitly. Implicity by using findViewById after a setContentView(R.layout.my_layout.xml) call (from the activity), or explicity by using it from an object (for instance, when you 'inflate' a layout and assign it to a View object you can do object.findViewById()).

Categories

Resources