I know I can set the layout in the program:
But I want adding dynamically components to an XML defined layout.
main.xml:
Error:
I want to assign the LinearLayout XML variable to a program
variable. findViewById is obviously a wrong method.
How Can I do it?
Code:
...
LinearLayout ll = new LinearLayout(this);
Button b = new Button(this);
ll.addView(b);
setContentView(ll);
...
main.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
...
Code:
LinearLayout xml_layout = new LinearLayout(this);
LinearLayout xml_layout = findViewById(R.id.linear_layout); //Type mismatch: cannot convert from View to LinearLayout
Button b = new Button(this);
xml_layout.addView(b);
setContentView(R.layout.main);
variable. findViewById is obviously a wrong method.
How Can I do it?