0

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

1 Answer 1

1

Don't use Application Context here: EditText edittv = new EditText(getApplicationContext());, just use specific Activity Context and it will solve your problem. I hope so :D!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.