I'm trying to create a RelativeLayout
with several children programmatically. To make the rules like RelativeLayout.RIGHT_OF
work, the child views must have proper IDs.
However, when I try to assign an ID, Android Studio flags it as an error:
view.setId(123);
ERROR: Expected resource of type id
How to Dynamically Add Views into View in Android? This example demonstrates How to Dynamically Add Views into View. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Add View Binding dependency inside the build.gradle (app) and click on the sync now button. .. Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file. Notice that it has a Linear Layout with id parent_linear_layout which we will inflate to add our views.
Here, the Text View is holding a simple text as "Todo List" and after that, the Empty Linear Layout having id of container, this linear layout is the place where we have to insert the EditText View programmatically, for now let it be empty. And then finally we have our "Add Task" Button to add the EditText in the empty Linear Layout.
Note that select Kotlin as the programming language Add View Binding dependency inside the build.gradle (app) and click on the sync now button. .. Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.
Found it:
view.setId(View.generateViewId());
You have two options:
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