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.
Related
The problem is that when I rename the id of any view like a button in the xml file, the name of the view doesn't get updated in the activity while using view binding.
When I rename the id, I use (refactor) or (shift+f6).
This is a big problem because when ever I rename an id in the XML file, it will break the code in the activity in case I'm using View Binding.
Example:-
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
when I change Button1 to Button2, the code breaks in the activity in case I'm using View binding.
Can anyone give me any way to properly rename the IDs so that the names in the activity get updated automatically while using View Binding.
Please, remember I do use (refactor) not f2.
If you agree with me that this is a very urging problem and it breaks our code, please vote this question up, so it will be suggested to more android developers. Maybe some already found a solution.
I saw a xml layout which has a textView as below
<TextView
android:id="#+id/tvHeaderTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Change password"
android:tag="#string/regular"
android:textAllCaps="true"
android:textColor="#color/header_color"
android:textSize="#dimen/font30" />
I want to know what is android:tag property is used for. A detailed answer for be greatly appreciated.
Note - I read that it is used for data binding, but could not follow the context.
Update 1 - (Thanks Blackbelt for the answer)
Usages
1. Could be used for compile time binding of xml elements with the activity. Although android recommends the use of id.
2. Could be used for identifying elements for list adapter. For eg, a special case of a list adapter with multiple sort support. Then we could use the tag element to identify the desired list item.
I would be glad to know if it could also be used for assigning any other property?
I want to know what is android:tag property is used for
it is a different way to add associate additional information with the view object itself. In you case your tag is a string, but it can store complex objects too. For instance the tag could be a model class set and retrieve at runtime using the setTag()/getTag() pair. The way this information is used is up to the users. Android allows to findViewWithTag too. In your case, for instance you could look for the same object using findViewById(R.id. tvHeaderTitle); or using findViewWithTag(getString(R.string. regular)); . An example of usage is with ListView when you have a button part of you item and, upon click you want to know the which item in your dataset is associated with that row. Using the pair setTag/getTag this thing is easily achievable
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.
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
This is probably a trivia question, but why are there package/class names in some people's XML layout files?
(please don't downvote this question if it is something trivial, i don't even know what this is called, so i couldn't look it up).
i was looking at a tutorial, and i saw something like this (in "sample.xml"):
<com.tutorials.foo
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- some buttons and views here -->
</com.tutorials.foo>
My questions are:
1) i'm assuming that "foo" is a custom view? say, like you want to extend TextView with your own version of TextView?
2) what is this pattern/technique even called?
3) what would be the advantages of using this method?
Thanks so much in advance!!!
Yes, <com.tutorials.foo .../> is a custom view.
Calling it will be as same as others.ex:
Foo foo=(Foo)findViewById(R.id.foo);
I assume you mean creating layout static(.xml) or dynamically with code. xml layout would be in advantage when you know that you will use this layout in the program and will not change its format. Of course you can add to it or edit it with code later on. It is also in advantage for readabilty.