0

Hy, I wanna ask something about dynamic EditText that's added by button click.

I have another scanner Button to scan qr code, get the value and set the value in each EditText added. To set the text I sure need to know the id of each EditText. So how do I assign id to each EditText or there's another way to work around with it.

Inside my add-button onclick

public void addView(View view){

    LinearLayout li = (LinearLayout) findViewById(R.id.etMsisdn);
        edt = new EditText(this);
        edt.setId(0);
        li.addView(edt);
}

Thank you very much

Dream Developer
  • 400
  • 5
  • 20
BrenDonie
  • 85
  • 1
  • 10
  • 1
    This post may help you: https://stackoverflow.com/questions/8460680/how-can-i-assign-an-id-to-a-view-programmatically – Antony Ng Aug 23 '17 at 07:10

2 Answers2

0

Hope to helpful

String strname = "task" + Integer.toString(i);   
EditText editText = new EditText(this);
editTextsMap.put(strname, editText);
Harshit Trivedi
  • 764
  • 9
  • 33
0

You can set a tag for your dynamically created EditText with calling setTag(Object tag) method. Then, you can simply find it with calling findViewWithTag() method.

// Dynamically create EditText
EditText editText = new EditText(this);
editText.setTag("editText");

// Find it via tag
EditText editText = (EditText) findViewWithTag("editText");
Ivan Panic
  • 166
  • 2
  • 6