Assign R.id to dynamically created editText

With thanks to stackoverflow, 2 methods, 1 simple and one less simple.

1) You can assign an id after creating the edit text and before adding it to the layout.

e.setId(int)

This doesn’t put it in the R.java file (so you can’t use findViewById(R.id.editTextId)) but you can use

findViewById(int)

where int is the same number used to set the id.

2)

Create a new xml file named ids.xml inside res/values folder.

Add new item like:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="button_group_cancel" />
</resources>

Now you can set the id to edit text as:

e.setId(R.id.button_group_cancel);

 

This entry was posted in Android. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *