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.

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