Reputation: 37
How would I implement a button such that a new TextBox is added dynamically on each click?
Upvotes: 1
Views: 13936
Reputation: 2370
You need to use EditText instead of TextView.
Hope this helps for you.
Upvotes: 0
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
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