16

So right now I have a text field with a button (add+) below it.

I would like to make it so every time text is entered into the Text Field, and the Add button is pressed, a new text view is added to a vertical layout below it with the text that the user typed in the field.

I do not want to simply make a text view invisible, then visible when clicked, because I would like them to be able to add more than one text view with whatever text they type.

user802609
  • 799
  • 6
  • 19
  • 34
  • Use addView method in ViewGroup class,it will help you for more info see:http://developer.android.com/reference/android/view/ViewGroup.html – sunriser Aug 03 '11 at 17:49

1 Answers1

35

This code contains what you want. (The view show an EditText and a Button, after you click on the button the text will add to the LinearLayout)

    private LinearLayout mLayout;
private EditText mEditText;
private Button mButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mLayout = (LinearLayout) findViewById(R.id.linearLayout);
    mEditText = (EditText) findViewById(R.id.editText);
    mButton = (Button) findViewById(R.id.button);
    mButton.setOnClickListener(onClick());
    TextView textView = new TextView(this);
    textView.setText("New text");
}

private OnClickListener onClick() {
    return new OnClickListener() {

        @Override
        public void onClick(View v) {
            mLayout.addView(createNewTextView(mEditText.getText().toString()));
        }
    };
}

private TextView createNewTextView(String text) {
    final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    final TextView textView = new TextView(this);
    textView.setLayoutParams(lparams);
    textView.setText("New text: " + text);
    return textView;
}

And the xml is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout">
 <EditText 
    android:id="@+id/editText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
 />
<Button 
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add+"
/>

kameny
  • 2,372
  • 4
  • 30
  • 40
  • how can we add a textview to another activity? I want to add a textview to my first activity from a button click in my second activity – CraZyDroiD Jan 04 '15 at 06:53
  • @KrauszLórántSzilveszter , Please read the following thread for further information http://stackoverflow.com/questions/3216294/programatically-add-id-to-r-id , – kameny Apr 10 '15 at 15:30
  • @kameny Do you happen to know how to add input text to a new textview when the enter key is pressed on a physical keyboard? – Samuel L. Jul 19 '17 at 18:56
  • How would you do this if the button was in a different fragment? – pewpew Sep 22 '17 at 18:55