Android: Is there anything good about giving id to all the views? - android

I'm studying Android by looking at open source projects code, and what I noticed was that, in many cases, they give ids to almost all the views, even if they don't actually use those ids in code.
For example, they give ids to LinearLayout, RelativeLayout and so on, but they don't really use those ids in code.
Is there any reason why they do that?
Because I think giving ids to all the views only makes me confused about what id was what view.
Is it just a bad practice?
Thank you in advance!

Is there any reason why they do that?
No.
Technically there is no reason to do that if you are not going to use the id anywhere (either in Java or in the XML file).
For eg it could be a personal choice, just like naming variables or naming class depending on its purpose (here note that this could be a personal choice).
From the developer API guides:
Defining IDs for view objects is important when creating a
RelativeLayout. In a relative layout, sibling views can define their
layout relative to another sibling view, which is referenced by the
unique ID.
Again,
Because I think giving ids to all the views only makes me confused
about what id was what view.
You can easily sort out the confusion thing if you give proper name to the id's.
Like:
btn_create for Buttons
tv_mobile_number for TextViews
ll_main_activity for LinearLayouts
rl_details_fragment for RelativeLayouts and so on.
Is it just a bad practice?
Technically not. But it may save you from the pain of typing id's for each and every view and may even save some time for you as well.
As a side note, I usually prefer giving id's only when needed.
Hope this will help you. Cheers!

sure it some times confused, in my case I give id only to views that I will use in codes.
thank.

Related

Is setting an id on views mandatory for all views like for instance a ConstraintLayout? [duplicate]

Is the android:id attribute required for all views in my layout? What is the best practice?
I would prefer to specify IDs in the rare instances that I need it (like x:Name in WPF), but it seems like Android and Eclipse like to have it on everything.
To put some more context in this, I believe the Android compiler automatically generates an ID attribute for every element (I could be wrong) in your xml layout file. But the purpose of having an ID attribute is so that you, the programmer, can either interact or refer to any given element.
For instance, you can set a layout (or table/row or...) to visible or invisible, or you can change the location of something on the screen...
Personally I don't bother putting an id on everything especially if my layouts spans several files and pages.
Views may have an integer id associated, but it's completely optional. You are going to need it if you want to be able to find specific views inside the view tree.
Moreover, ids need not to be unique across the entire view tree, but just in the part you are searching.
No. It's only important if you are putting the view in a RelativeLayout(and will use it to position other views) or want to get it via findViewById(), but it is not required.

Android: leave out unused ids in layouts?

i would like to know if it's bad practice when you don't assign an ID to a View, which isn't referenced programmatically, within your xml layout?
Thanks!
Philipp
If a view neither needs to be referenced inside your code nor needs an ID for positioning purposes in the XML layout definition (for the android:layoutAbove/Below etc. properties of child views inside a relative layout), then there is no reason for you to assign an ID to it. I would say generally, it is considered good practice not to assign it, because the IDs you assign will be added to your project's R file, causing unnecessary bloat, however small it may be.
No.
If you are not going to use the view from code its better to not give it an I'd.
here are a few reasons:
Less xml code.
Less options on auto complete with the R.id.

Dynamic Views in a RelativeLayout

This question is more about best practices.
I'm working on some code that creates a UI from a dynamic XML file. It took me quite a while to discover that Dynamic Views have an ID of -1. Which means if you want to layout Dynamic Views in relation to other Dynamic Views you need to give them IDs on creation.
So I did that, but I don't really like the solution I came up with, and there's a chance that I could accidentally give the same ID to two elements.
Is there a way to let Android assign unique IDs to these dynamic views, or do I have to create some sort of ID tracking code myself?
Taken from here
From API level 17, you can call
View.generateViewId()

Is it bad to remove ID from views?

If you have a layout that has a bunch of views that won't be changed during runtime, for example a TextView used as a label, is it proper to remove the ID from it, or to label it like a view that you would use?
What about layouts? If I have a bunch or table rows in a table, should each have a unique ID or should I clear the field?
It seems to me like it removes a lot of clutter if I clear the IDs if views that I won't be changing but I don't want to do that if it's bad practice.
Thanks.
You only need to define an id for an UI component if you want to reference this component later from your program code (e.g. findViewById(R.id.my_textview) ).
Because you said the views won't change during runtime you don't have to define an id for every view.
It can even help you if you don't define an id, because it keeps the auto complete function of your IDE clean. If you have a lot of layouts (which contain lots of ui elements) and you define an id for each component then you will have a nearly endless list of component ids at the end.
#matt: if your are using canvas to draw the views and set layouts then id is not necessary from my knowledge and I never used id fro and I think its not bad practise.

Does R.java force all my layout elements to become public variables based on their IDs?

I assumed that each layout's element id's such as buttons, textviews, edittexts, etc. were unique and private to that layout. This being said, you set the Activity to use a certain layout, you should only be able to find view id's based on the id's set in that specific layout.
However, I am finding now that I can reference whatever view id I want from my Activity regardless of the physical layout I have specified with "setContentView(R.layout.THELAYOUTIWANTTOTARGET)". Is this behavior normal, I figured only id's of those elements on the layout I specified above would be available? Looking at R.java, I believe all the id's I specified on all layouts are made public variables, thus, no id's can have the same name or unexpected behavior may occur!
The implications for this on my project is that I must now change all id's in all layouts to be unique. I figured my supplier layout > title textview would have been different from my customer layout > title textview, is this not the case?
Thank you for clarifying!
Your assumption is correct. IDS are global, and for large projects it's sometimes a pain, leading to very long ID names. But that can also be used as an advantage, as you can reuse layouts on different activities.
For example, you can have a layout for a specific part of your activity (a custom buttons bar for example) that you may want to add to several activities. In this case, you can just inflate it into a specific ViewGroup of the first activity, and also in another ViewGroup in another activity. The methods to access specific buttons based on their ids can then be reused in both activities.
The method findViewById will only work on the activity you call it from. If a button (or any other component) exists on different activities, only the one in your current activity will be returned.
Yes, it is normal. As far as I know, there is no way to change it.
So yes, the implication that you will have to have different names for views in different layouts is correct.
EDIT:
Actually... scratch that. I was under the impression that it was necessary, but according to
http://developer.android.com/reference/android/view/View.html
it isn't even necessary to have id's be unique in a single file. Just make sure you aren't searching in a tree that has multiple ids that are the same or you will always get the first occurrence.
It makes sense too since it doesn't really matter if the views have the same ID in R. I will keep this in mind going forward.

Categories

Resources