Dynamically add a Edit text

Keep an EditText item in your layout and set its visibility to gone.

 <EditText android:visibility="gone" android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

Then on the onClick event of the button set visibility to visible.

or

You can add the EditText items programmatically.

Add a LinearLayout to your xml.

<LinearLayout
        android:id="@+id/editTextGroupLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

On button click event add EditText programmatically.

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.editTextGroupLayout);
    EditText editTextView = new EditText(this);
    editTextView.setGravity(Gravity.CENTER);

    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT, 1);

    editTextView.setLayoutParams(params);

    linearLayout.addView(editTextView);

Hope this would help.

Leave a Comment