Can I use the same id on different xmls? - android

I have about 20 different xml files, and all of them has a raitingBar on them, and I have only one Activity for all of them, so my question is , can I use the same id for all of the raitingBar on the xml files?

Yes you can, In Android all specified XML IDs finally at build time will be some integers in R.id.* class.
For example suppose you create this XML item:
<TextView android:id="#+id/text"
... />
The statement android:id="#+id/text" tells Android that add a new (because of + sign) ID called text into the R.id class. For example it takes some iteger like 123.
At this point, you define another view in another XML file with the same ID :
<TextView android:id="#id/text"
... />
Now in your Java codes, for example you use layout_1.xml (the first one) and then making a call findViewById(R.id.text). Keep in mind that all R.id.* is type of Integers (those are not some strange objects !)
So this statement is interpreted as findViewById(123). If you remember you've used only layout_1.xml file and in this file only one view exists with the ID R.id.text = 123. So this call will target only one view at the layout_1.xml not all your views in another XML files. That's it :)
Note: Try avoiding use same ID in same XML file.

Related

Creating id for Android Views

This question is quite basic. Let me go with an example.
I have two activities
activity_a.xml
ActivityA.java
activity_b.xml
ActivityB.java
Both the XML files contain only a TextView to display a simple text. As usual, the TextViews are going to be referenced in the corresponding .java files using their View id
My question is, if it is right to reference the TextView in both the XML files with same id? (like using the below code with exactly same id for activity_a.xml and activity_b.xml)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I was practising this procedure without any problems. When trying to reach the corresponding xml code for the TextView using Ctrl + Click (on Windows), I am provided with two options (to display the TextView's xml code from activity_a.xml or from activity_b.xml).
Also, what is the recommended way to name a View in Android? This will be helpful, when your Android project contains multiple layout files.
Yes , if you have same name of view or view group in different layout then it tells from which layout file it belongs to, ask for select required layout file.
so for that you have to follow proper naming conventions to avoid this type of confusion
https://github.com/ribot/android-guidelines/blob/master/project_and_code_guidelines.md
or you can give name like
activity_home_tvUserName if username textview from home activity
and
activity_profile_tvUserName if username textview from profile activity.
if it is right to reference the TextView in both the XML files with same id?
It is totally fine, the compiler will only look at the ID under a single view hierarchy.
e.g.: findViewById(R.id.textview) inside ActivityA.java will only search for textview ID inside activity_a.xml (assuming you have setContentView(R.layout.activity_a); beforehand.
what is the recommended way to name a View in Android?
In my opinion, you just need to be consistent in naming your view throughout the app. The main goal is to avoid misinterpretation and confusion.
Hope it helps!
They are in different activities, so you should have no problem using the same id. you could event declare them both as #+id/textview. However, why not just use the same XML file for both activities? No reason you can't.
You can also create an ids.xml file under the values folder and declare all your ids under it, so you don't have to declare them in your layouts, but this is not a very common approach.
if it is right to reference the TextView in both the XML files with same id?
My answer is Yes, It is right.
Whenever we set the setContentView(R.layout.activity_a), then it'll search for the given id within the above activity. Local attribute having the same id will take the more preference over the other attributes with the same id.
But having unique id's is Best practice.

android layout id naming: use of R.group instead of R.id

I've seen that all the layout ids are identified by #+id/viewid, but it seems the #+my_group_name/viewid works as well. Is it normal to use this naming convention, or the id must be in the "id" class ? I've checked the forum and found prefix based naming conventions but nothing like this.
In examples: I have a dialog layout called dlgeffect. In this layout:
<CheckedTextView
android:id="#+dlgeffect/text"
android:layout_width="0dip" ... >
I now the id's are reused, but during layout modifications if the ids are unique for the whole project the compiler gives error this way (and not runtime error)
Thanks,
Gzp
EDIT: and of course from java it is referenced as R.dlgeffect.text
I really dont know if you can do this, but always stick to standards, if you want it for reuse porpuse or setting property for more than element at once you can use same id for different element

why #+ sign in android , please , explain this

I am not getting what Author wants to explain , so please explain this , "Use #+ on the first occurrence of a given android:id in a layout (XML) file. It might be in the definition of the view or it might be in a reference - which ever is first. In the example above, the EditText view is defined before the Button. So the #+ is used on the android:id attribute in EditText. However, if the Button was defined first, the #+ would be used on the relative layout positioning in the Button" ,
..
Read more: http://www.intertech.com/Blog/Post/Android-Layout-and-ID-Attribute.aspx#ixzz2MHHdt1wv
#+ means, that if this id doesn't exists yet, it will be created, otherwise already created id will be used. If you look at R.java - ids is some numeric constants.
If you write just # - you should be sure, that id already created.
Take a look here. It explains a lot, take a look at the ID section.
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"

When should you use `#+id` instead of `#id`?

I have a bunch of Views in a <merge>, and I included that <merge> into a RelativeLayout. I try to refer to the IDs of those included Views to act as anchors for my other Views, but Eclipse complains that the IDs are not resolving. I found a workaround by using #+id rather than #id when I first refer to them rather than when I actually define the objects they refer to. I've already defined the two IDs in a Style and in the included <merge> where they are declared, so it feels a bit inefficient if I keep repeating the definition of the ID.
Is this the correct way of doing it? I'm assuming it's bad cause the '+' is another initialization. My current hypothesis is that you should use #+id when you first use the ID rather than when you initialize the object that the ID is going to represent, a bit like C/C++ and how they require at least a function prototype in the lines prior to the actual code that uses the function.
Another question I have is when you use the GUI-based UI builder of Eclipse, I noticed that they always use #+id rather than #id. Is this acceptable, cause it seems inefficient to me; it's as if the application will be spending more time determining whether or not the ID has been declared in R.id.
Using #+id format tells the Android asset compiler to assign an ID to your element, it isn't actually an id itself. So if I use #+id/myNewId the asset compiler will create a new id named myNewId and provide a number for it. The actual number can be accessed from your code as R.id.myNewId.
If you use an #id, the compiler will look for R.id.id. You can define your own id's in XML files, as explained here: http://developer.android.com/guide/topics/resources/more-resources.html#Id. You could create your own file in res/values/[your_filename].xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="id_name" />
</resources>
and then refer to #id_name, for e.g.
You can also use the Id's defined in the Android namespace: #android:id/empty
This is well explained in the Android documentation: http://developer.android.com/guide/topics/ui/declaring-layout.html#id
There's also some further discussion here: android:id what is the plus sign for

Difference between android:id, android:name and name tags in Android XML files

Can someone clarify the difference between android:id, android:name and name tags in Android's XML files. They all seem to be ways to reference things.
For example, when I have a string array in the res/values/array.xml file I access using the name field in the defined array yet the Javadoc refers to this as the "ID".
android:id seems to be just used in Views ?
Am I missing something or would it not be simpler to have one tag?
No, I don't believe you're missing anything. Although these fields are named differently, it's my understanding that they are both used to identify/reference resources. This specifically means GUI elements (views) in the case of android:id and static resources in the case of name.
To go into more depth, I believe the android:id attribute is assigned only to Views and classes that extend View. This is done so that the view can be programmatically accessed from your code using findViewById:
Button myButton = (Button) findViewById(R.id.whatever_id_assigned_to_view)
This is different from resources such as strings.xml or array.xml which are identified simply by name, such as a following example of what might be found in strings.xml:
<string name="string_name">Text Resource Here</string>
and is accessed using...
getResources().getText(R.string.string_name)
I imagine these are separated for organizational reasons. This way the generated Android resource file (R.java) contains the IDs specified for views in R.id, the string IDs contained in R.string, array IDs in R.array etc.

Categories

Resources