I try to create program in the code. There was a need to set a specific ID for each twist. I read the following article: https://stackoverflow.com/questions/3216294/programmatically-add-id-to-r-id

Here, for example, I have

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> 

How to add a new ID there programmatically ??

    1 answer 1

    Not. You cannot add new resources programmatically. ID is an int type number. You can use any number as an identifier for your views.

    For example, defining it as a constant:

      public static final int MY_ID = 100; 

    You can create views like this:

      for (int i=0;i<1000;i++) { View view = new View(context); view.setId(MY_ID + i); layout.addView(view); } 

    And then look like this:

      layout.findViewById(MY_ID+58); 
    • And if I have a large number of views, how can I search for them later? And if I don't know the exact number of these views, how will I assign these id? - Snuf
    • Updated the answer. If you do not know the exact number of views, you can create a variable to store the last id used. - KosWarm