Search for layouts by ID

Noob is here.

I am trying to call an image on a button to do this, I took the code from this question: How to create an ImageView in java code, within an existing layout? I used the code from the first answer, but in the line:

RelativeLayout rl = (RelativeLayout) findViewById(R.id.RelativeLayout01); 

I do not know what to put in place of "RelativeLayout01", or how to have a layout in "id"

Thank you in advance

+6
source share
2 answers

findViewById() is a method that you call in a view that you have already inflated from an XML file (see this question for more information on inflating views; also the documentation for findViewById ).

"RelativeLayout01" in this example refers to the identifier specified in the main layout in the XML file associated with the Activity that contains the click listener that you are writing. It is just a placeholder; set the identifier on the main layout in the Activity XML file and use this code in the code to place the image.

Also, if you haven’t done so yet, read the documentation for XML layouts ; which should clarify some things. The android:id attribute (see Sample XML file, the attribute can be applied to any element) is the one that relates to your question.

+6
source

After entering R.id.r, press CTRL + space on the eclipse, it will try to autocomplete if you see the variables in the list, and then select them, if not, then you did not add them to the XML.

0
source

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


All Articles