0

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

editText.setId(createdID);

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!

Nina Seil
  • 43
  • 5
  • Why do you want to set IDs when you are creating `EditText`s in code? Why can't you keep hold of references? – waqaslam Feb 14 '18 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 '18 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 Seil Feb 14 '18 at 21:33
  • Okay, but can the int-ID be a int-variable? – Nina Seil Feb 14 '18 at 21:34

2 Answers2

0

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?

Volshebnik
  • 61
  • 9
  • This is so easy! And it works perfectly! Create ID with: int createdID = View.generateViewId() -- (API level 17 and above) – Nina Seil Feb 15 '18 at 10:06
0

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

600
  • 84
  • 3
  • I edited my question with more detail. Is this possible with tags as well? – Nina Seil Feb 14 '18 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? – 600 Feb 15 '18 at 07:54
  • What do you mean with EditText(context)? – Nina Seil Feb 15 '18 at 09:52