Ahsan
Ahsan

Reputation: 215

Assign R.id to dynamically created Edit Text

I am new to this. wanted to know if there is a way to assign R.id to dynamically created edit text so i can move the data with in to SQLite db. tried few diff ways but fail. Any help would be appreciated. Thanks

Upvotes: 2

Views: 1439

Answers (2)

Tarun
Tarun

Reputation: 13808

In android id should be unique. Setting any arbitrary integer value can lead to duplicate ids. The correct way is to define id like below.

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 edittext as:

edittext.setId(R.id.button_group_cancel);

Upvotes: 2

TronicZomB
TronicZomB

Reputation: 8747

You can assign an id using .setId(int) before you add the EditText to the layout. However this does not place the id into the R.java file.

You can reference the EditText with findViewById(int) instead of using findViewById(R.id.editTextId), where int is the same int used in the set method.

Upvotes: 2

Related Questions