i want to dynamically add views (textview or EditText) to my layout as per user instructions. i am developing this just to figure out things and learn how they work. my XML file is as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/email"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="email1"
/>
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter mail "
/>
</LinearLayout>
<LinearLayout
android:id="@+id/phones"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="phone number"
/>
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter phone "
/>
</LinearLayout>
<LinearLayout
android:id="@+id/myButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add mail"
android:onClick="addmail"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add phone"
/>
</LinearLayout>
</LinearLayout>
now here , i have two buttons. when the user clicks on addmail i want an edittext to be added to my layout (below my first email EditText in LinearLayout with id "email". i have set android:onClick to addmail for the button and my addmail function is as follows:
public void addmail(){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((LayoutParams. WRAP_CONTENT) , (LayoutParams.WRAP_CONTENT));
EditText edittv = new EditText(getApplicationContext());
edittv.setLayoutParams(lp);
LinearLayout ll1=(LinearLayout)findViewById(R.id.email);
ll1.addView(edittv);
}
but the code is not working . what modifications are required? where am i going wrong. Are my concepts at fault? thankyou in advance