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
FindViewById(Int32)Finds a view that was identified by the android:id XML attribute that was processed in #onCreate .
For example, when you specify an ID to an XML resource using the plus sign — in the format android:id="@+id/myView" —it means that a new ID resource with the name “myView” needs to be created, and if it does not exist, create a unique integer for it and add to R. java .
Layout files are stored in "res-> layout" in the Android application. When we open the resource of the application we find the layout files of the Android application. We can create layouts in the XML file or in the Java file programmatically. First, we will create a new Android Studio project named "Layouts Example".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With