Can someone explain the magic behind FindViewById (1) or FindViewById (2)?

[UPDATE] - I just wanted to confirm to everyone that I agree with it. ALWAYS a bad idea to use hard code. My curiosity was to use the Maps API, for which a constant value of 1, 2 is indicated. I would feel more comfortable if I found something like R.id.my_current_location_view.

I see that this code has settled in this question and other questions. How do people know what to use findViewById (1) etc.? against findViewById (R.id.something);

Reposition the Google Maps API and My Location. Button

There seems to be some deep confusion on this issue. I am wondering what the magic numbers of findViewById (1) are .. If you click on the link, you will see that no one has set setId (1) in mapView, but somehow people know to check the parent view and then findViewById ( 1) and findViewById (2) ..

I also asked the question so that the context is not lost, but no one seems to be able to explain this.

+5
source share
4 answers

It is a common bad idea to use this approach with hard-coded identifiers, especially if you are not the one who sets them, as they can be changed.

However, in order to answer the question of using id to use, one of the options is to make a lot of guessing and test it using the Android Studio debugger to manually move the view hierarchy. This is likely to take a lot of time, so it’s much easier to use just the Hierarchy Viewer , which is available in both Android Studio and Eclipse.

This is what the part of the layout we are interested in looks like. I cropped the full layout to show the corresponding part.

enter image description here

Now we can break the following code:

View locationButton = ((View) mapView.findViewById(1).getParent()).findViewById(2); 
  • findViewById(1) will return a LinearLayout (red outline) since its identifier is 1.
  • getParent() will return a RelativeLayout , which is the parent of a LinearLayout (Blue Outline).
  • findViewById(2) will find the child RelativeLayout with the given identifier. Based on the image, we see that the ImageView (Green Outline) we want has this id.

Based on this, it is interesting to note that we could just use mapView.findViewById(2) to directly access ImageView and avoid other calls.

Not sure if this was in previous versions, but it works when using the current MapFragment . If this does not work in previous versions, it helps to prove that this is bad practice, as it can vary between versions.

+1
source

View ids are simply positive, nonzero integers.

If you set the view identifier in XML using the syntax @+id/name , an integer is generated in R.java , and you need to refer to it as R.id.name in the code.

If you set the view identifier in the code using setId(value) , you can get a view with that identifier from the hierarchy using findViewById(value) , where the id value can be a hard-coded positive non-zero integer such as 1 or 2 .

As a rule, when writing code, try to avoid using such magic constants.

+5
source

they are both the same, in the first case you supply the resource identifier that android builds in the R file yourself, and in the second case you provide the resource identifier that android compiled for you.

no difference,

0
source

First you need to set the id view using setId(value) , and then you can get the view using id using findViewById(value)

Found this post by looking at it: Link

The documentation for setid() says:

Sets the identifier for this view. The identifier does not have to be unique in this view hierarchy. The identifier must be a positive number.

0
source

Source: https://habr.com/ru/post/1202734/


All Articles