Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically add id to R.id

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?

like image 427
Ben Avatar asked Jul 09 '10 20:07

Ben


1 Answers

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.

res/values/ids.xml

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

To use it in code:

myEditTextView.setId(R.id.my_edit_text_1); 
like image 183
Hidden Android Avatar answered Sep 20 '22 20:09

Hidden Android