I have an Activity in my App that adds more EditText fields to my view when a button is clicked.

Now I want to create a new ID for every created EditText-Field. Then I want to use


And then I want to add this editText to an ArrayList (thats why I need the ID!)

editTextList.add((EditText) findViewById(createdID));

Any ideas? Thanks!

  • Why do you want to set IDs when you are creating EditTexts in code? Why can't you keep hold of references?– waqaslamFeb 14 at 20:34
  • What you are asking is not possible. Yes, you can set ID programmatically but it has to be an int, it can't be a String. You can't do "edittext" + String.valueOf(3) for example. When you set IDs in XML, it all becomes int eventually– ᴛʜᴇᴘᴀᴛᴇʟFeb 14 at 20:37
  • I cannot define the ID in references, because I dont know how many text-fields are created by the user (could be 40 etc)..– Nina SeilFeb 14 at 21:33
  • Okay, but can the int-ID be a int-variable?– Nina SeilFeb 14 at 21:34

You can set it with TextView.setId(int id).Try to see this question for more details.Android: View.setID(int id) programmatically - how to avoid ID conflicts?

  • This is so easy! And it works perfectly! Create ID with: int createdID=View.generateViewId() -- (API level 17 and above)– Nina SeilFeb 15 at 10:06

Rather confusing idea. But if you really need - you can set tags to yours EditTexts

  • I edited my question with more detail. Is this possible with tags as well?– Nina SeilFeb 14 at 21:32
  • Well, according with your edited question - for what you are need ID here? Just add yours edittext: list.add(new EditText(context)). But i think you should also dynamically add it at layout, right?– 600Feb 15 at 7:54
  • What do you mean with EditText(context)?– Nina SeilFeb 15 at 9:52

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.