Android: Set Edit text or text view id Programmatically

As the others have said you can’t do this. Why do you want to / what’s your requirement?

You could create an id in an xml file and use that – if you want it to be descriptive. This is also a better approach than using literal ints as you may get an id clash with other views in the layout hierarchy (unlikely but possible). This to me seems like the best / cleanest solution to your problem.

See http://developer.android.com/guide/topics/resources/more-resources.html#Id

e.g. in res/values/id.xml

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

and then set with

edText.setId(R.id.edittext_hello);

Leave a Comment