Creating and adding a new View to a Layout
XML example – Adding to a view in a layout XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainLayout" >
<TextView
android:text="My text view"
android:id="@+id/TextViewExample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
Code example – Creating and adding a new view to a Layout (implemented in an Activity class).
TextView textView = new TextView(this);
textView.setText("My Text View");
((LinearLayout)findViewById(R.id.mainLayout)).addView(
textView ,
new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
