Skip to content Skip to sidebar Skip to footer

Dynamically Add A Edit Text

I want to add an edit text field to my app when i click a button, but i do not change the layout. Just add it below an existing one defined by the xml file. Lets say i have a conta

Solution 1:

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

<EditTextandroid: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.

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

On button click event add EditText programmatically.

LinearLayoutlinearLayout= (LinearLayout) findViewById(R.id.editTextGroupLayout);
    EditTexteditTextView=newEditText(this);
    editTextView.setGravity(Gravity.CENTER);

    LayoutParamsparams=newLayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT, 1);

    editTextView.setLayoutParams(params);

    linearLayout.addView(editTextView);

Hope this would help.

Solution 2:

What you can do is have those EditText's in a panel, i.e. group them in panels, then have a button, and on button click create an instance of an EditText and add it to the relevant panel. This way you are not limiting yourself to being only able to add one panel, but as much as the user would like to add.

Post a Comment for "Dynamically Add A Edit Text"