Finding layouts by id

19,167

findViewById() is a method you call on a view you've already inflated from an XML file (see this question for a little more detail on inflating views; also, the documentation for findViewById).

The "RelativeLayout01" in this example refers to an id set on the main layout in the XML file related to the Activity that contains the click listener you're writing. It's just a placeholder; set an id on the main layout in your Activity's XML file, and use that id in your code to place the image.

Also, if you haven't already, read the documentation on XML layouts; that should clear up a few things as well. The android:id attribute (see the sample XML file; the attribute can be applied to any element) is the one relevant to your question.

Share:
19,167
Sochimickox
Author by

Sochimickox

Updated on June 04, 2022

Comments

  • Sochimickox
    Sochimickox almost 2 years

    Noob here.

    I am trying to spawn an image on a button click, in order to do that I took some code from this question: How do I 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 don't know what to put instead of "RelativeLayout01", or how to have a layout in "id"

    Thanks in advance