-2

I am new to android. Now i am need to know how to get text values of all edittext which has added dynamically when user press add button.

add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
table = (TableLayout) findViewById(R.id.table);
row = new TableRow(Material.this);
table.setGravity(Gravity.CENTER);
 edit = new EditText(Material.this); 
 edit.setWidth(135);
 edit.setHeight(35);
 edit.setBackgroundColor(Color.WHITE); 
 row.addView(edit); 
table.addView(row); 

Here how to get text values of edittext? User may add more than one edittext. At that case, how to get all text?

5 Answers5

2

If EditTexts are dynamically added maintain the references of EditText in an array and when ever you want text from them iterate the array.

Chandrakanth
  • 3,711
  • 2
  • 18
  • 31
1

If you have created edit text and text is set in edit text then:

String text = edittext.getText().toString();

would be okay.

Khushal Chouhan
  • 581
  • 5
  • 15
1

EditTexts are created dynamically with a reference use that reference to get the value of it.

For Example

I have created an editText.

EditText myTextBox = new EditText(getBaseContext());
containerLayout.addView(myTextBox);

Here, myTextBox would be the variable which refers to that editText, you could use getText() to get the value of that box or you could use setText() to set the value to that box.

String value = myTextBox.getText().toString();
myTextBox.setText("this is the setted text");`

EDIT

If there are more than one editText then add all the references in an array and iterate over them.

List<EditText> myArray = new ArrayList();
EditText editText1 = new EditText(getBaseContext());
 containerLayout.addView(editText1);
 myArray.add(editText1);

EditText editText2 = new EditText(getBaseContext());
 containerLayout.addView(editText2);
 myArray.add(editText2);

EditText editText3 = new EditText(getBaseContext());
 containerLayout.addView(editText3);
 myArray.add(editText3);
 int i = 0;
    while (i < myArray.size()) {
        Log.i(TAG,myArray.get(i).getText());
        i++;
    }

enter image description here

Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
0

You will need to keep reference of that dynamically added EditText.

And call getText().toString(); on that reference.

One other approach is to set some ids using setId(); keep record of those ids and whenever required.

((EditText)findViewById(edittextidyouset)).getText().toString();

Take look at this thread setting ids dynamically : How can I assign an ID to a view programmatically?

Community
  • 1
  • 1
Prashant
  • 4,474
  • 8
  • 34
  • 82
0

Hope this can help you-

ArrayList<EditText> editTexts = new ArrayList<>();
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.container);
        for (int i = 0; i < 20; i++) {
            EditText editText = new EditText(this);
            editTexts.add(editText);
            editText.setText("I am at " + i);
            linearLayout.addView(editText);
        }
        for (EditText editText : editTexts) {
            Log.d("Texts", editText.getText().toString());
        }

Your xml file can be like-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/container"
    android:orientation="vertical"
    tools:context="com.hestabit.activity.Home">


</LinearLayout>
Sanjeet A
  • 5,171
  • 3
  • 23
  • 40