Reputation: 135
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
Upvotes: 9
Views: 21357
Reputation: 39201
After you type R.id.r press CTRL+space on eclipse, it will try to autocomplete, if you see your variables in the list, then select them, if not then you didn't add them to XML.
Upvotes: -1
Reputation: 1553
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.
Upvotes: 8