Ripu Flora
Ripu Flora

Reputation: 37

How to dynamically add edit text boxes when button is clicked?

How would I implement a button such that a new TextBox is added dynamically on each click?

Upvotes: 1

Views: 13936

Answers (3)

kameny
kameny

Reputation: 2370

This is my earlier post.

You need to use EditText instead of TextView.

Hope this helps for you.

Upvotes: 0

Dimitris Makris
Dimitris Makris

Reputation: 5183

You should have something like this:

Button mButton = (Button) findViewById(R.id.my_button);
mButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        EditText t = new EditText(myContext);
        t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        root.addView(t);
    } 
});

root: is the root layout where you want to add the EditText.

myContext: could be the activity, etc, etc.

Hope this helps!!

Upvotes: 0

Glenn Bech
Glenn Bech

Reputation: 6182

If you only, and always want to add two Edit Text widgets to your activity when the button is pressed you can do something like this (pseudo code). This assumes that you never want to have more than two edit text components beside your button.

<LinearLayout orientation="horizontal">
   <Button >
   <EditText id="@+id/et1" visibiltiy="gone" />
   <EditText id="@+id/ed2" visibiltiy="gone" />
</LinearLayout>

in your button's onClick listener you can change the components visibility to visible by calling

findViewbyId(R.id.et1).setVisibility(Visible)
findViewbyId(R.id.et2).setVisibility(Visible)

Upvotes: 1

Related Questions