Sonia John Kavery
Sonia John Kavery

Reputation: 2129

Get Input value from Edittext which generated dynamically

I have to create n EditText where n is user input.

I could create it using for loop.

TableLayout tbl=(TableLayout)findViewById(R.id.TableLayout1);
    //table row
    for (int i = 0; i < 5; i++) {
        TableRow tr = new TableRow(this);
        TableLayout.LayoutParams tableRowParams=
                new TableLayout.LayoutParams
                (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
        //for set margin
        tableRowParams.setMargins(0, 10, 0, 0);
        tr.setLayoutParams(tableRowParams);
        tr.setGravity(Gravity.CENTER_HORIZONTAL);
        //text view
        TextView tv=new TextView(this);
        tv.setText("Field "+(i+1));
        tv.setGravity(Gravity.CENTER);
        tv.setTextColor(Color.parseColor("#0070C0"));
        tv.setTextSize(26);
        tv.setLayoutParams(new TableRow.LayoutParams(100, TableRow.LayoutParams.WRAP_CONTENT));
        //add textview
        tr.addView(tv);
        //set layout params of edittext
        TableRow.LayoutParams etParams=
                new TableRow.LayoutParams
                (120,30);
        etParams.setMargins(10, 0, 0, 0);

        EditText et=new EditText(this);
        et.setLayoutParams(etParams);
        et.setBackgroundResource(R.drawable.bg_grey);
        et.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        tr.addView(et);
        tbl.addView(tr, tableRowParams);
    }

I got Layout

enter image description here

Let me know how to retrieve data from dymamically created EditTexts.

Upvotes: 2

Views: 2282

Answers (3)

MadDeveloper
MadDeveloper

Reputation: 66

to add editText

//list to store edittexts
List<EditText> etList = new ArrayList<EditText>();
TableLayout tbl=(TableLayout)findViewById(R.id.table);
for(int i=1;i<=5;i++){
    TableRow tr = new TableRow(CurrentActivity.this);  
    TableLayout.LayoutParams tableRowParams=
        new TableLayout.LayoutParams
        (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);       
    EditText et=new EditText(CurrentActivity.this);
    et.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_CLASS_NUMBER);
    et.setId(Id+i);//string+i
    //add et to table row
    tr.addView(et);
    //add table row to table            
    tbl.addView(tr, tableRowParams);
    //add edittext to list
    etList.add(et);
}

to get values

for (EditText et : etList) {
    System.out.println(et.getText().toString());
}

Upvotes: 5

Shadik Khan
Shadik Khan

Reputation: 1215

In you layout xlm create LinearLayout

in Activity onCreate method find that layout

containerLayout = (LinearLayout)findViewById(R.id.LinearLayout1);

//now create loop
for(int i=0; i<=arraysize; i++){

EditText editText+String.valueof(i) = new EditText(getBaseContext());
    containerLayout.addView(editText+String.valueof(i));
      editText.setGravity(Gravity.Left);
     LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) editText.getLayoutParams();
      layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
      layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
       // layoutParams.setMargins(23, 34, 0, 0);
     // RelativeLayout.LayoutParams()
       editText.setLayoutParams(layoutParams);
       //if you want to identify the created editTexts, set a tag, like below
     editText+String.valueof(i).setTag("EditText" + i);

}

Upvotes: 0

Deutro
Deutro

Reputation: 3323

As you create the EditTexts you should store them in an Array or in a List

for (int i = 0; i < 5; i++) {
    EditText myEditText = new EditText();
    myEditTextList.add(myEditText);
}

After this you can just iterate over your list and get the strings from the EditTexts:

for (EditText editText : myEditTextList) {
    String text = editText.getText().toString();
    Log.d(TAG, text);
}

Upvotes: 1

Related Questions