views:

97

answers:

2

i am creating an EditText object which i then try to reference in a unit test. what is the best way to add a new 'id' to R.id for this dynamically created object so that i can later reference it via findViewById() in the unit test?

A: 

You cannot modify the R class at runtime.

You can call setId() on your EditText object and give it some ID value, though.

CommonsWare
i dont necessarily need to edit it at runtime, can i just add the ID somewhere in an xml file? if so what would be the best place?
Ben
Either put the `EditText` in layout XML file like all the rest of your layouts (and assign it an ID via `android:id`), or create it at runtime (and assign it an ID via `setId()`).
CommonsWare
+2  A: 

You can set ID's you'll use later in R.id class using an xml resource file, and let Android SDK give them unique values during compile time. For example:

------------ res/values/ids.xml ---------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <!-- id's which will be in R.id and you'll use later -->
  <item type="id" name="my_edit_text_1" />
  <item type="id" name="my_button_1" />
  <item type="id" name="my_time_picker_1" />
</resources>
------------------------------------------------

And later you just use them: myEditTextView.setId( R.id.my_edit_text_1 );

Hidden Android