title: Android - Set my own unique integer id (not in resources) to many views created programmatically
body: <p>I want to give ids to multiple views created programmatically and each id to be a specific integer based on my own logic. Consider the following example:</p>
  
  <ol>
  <li>I have M colors defined in a sequential way, e.g. using enum, or using an array to put M color values. (Which implementation/structure to use is part of the puzzle and suggestions need to be provided for this as well, in order to achieve the final goal described below.)</li>
  <li>Create N instances of a custom Class and store them in a sequential way too (e.g. an array of N elements). This custom Class will just have a member mColor where a color for the specific instance will be stored.</li>
  <li>Create (programmatically always) N RadioGroups with M RadioButtons for each RadioGroup.</li>
  <li>The goal is when user clicks the j-th RadioButton of the i-th RadioGroup, then the i-th instance will use as background color the j-th color.</li>
  </ol>
  
  <p>So, I would like to do something like this when OnCheckedChanged event occurs for a RadioGroup:</p>
  
  <pre><code>public void onCheckedChanged(RadioGroup group, int checkedId) {
      int i = group.getId();
      /*j variable is not actually needed here, but is used for concistency
      with the description above.*/
      int j = checkedId;
  
      /*assume for simplicity that an array of N elements is used to store
      the N CustomCLass instances and an array of M elements is used for 
      colors*/
      customClassArray[i].setmColor(colorsArray[j]);
  } 
  </code></pre>
  
  <p>From the above, I think it is necessary that specific ids for the programmatically created RadioGroups and RadioButtons need to be set. If there is a way for this to be done, then code will be clear, optimized and no switch statements will be needed for N RadioGroups and M RadioButtons (for which, in any case, I have no clue about how it could be implemented too).</p>
  
  <p>Which is the best way to achieve this, firstly with efficiency in mind and secondly, with clear code in mind?</p>
  
  <p>Thank you in advance for any suggestions.</p>
  
  <p><strong>Edit:</strong> For clarification, I have read about setting my own ids by using View.setId(), but there are many resources on the web which are against this hardcoded approach (and I also vote against this if there is another way) and many of them suggest using xml resource to put your ids. However, as I said earlier, I think this is not what I want in order to achieve my goal.
  Example links suggesting this approach: <a href="https://code.google.com/p/android/issues/detail?id=75081" rel="nofollow noreferrer">https://code.google.com/p/android/issues/detail?id=75081</a> or also  <a href="https://stackoverflow.com/questions/8937380/how-to-set-id-of-dynamic-created-layout/8937477#8937477">How to set Id of dynamic created layout?</a>.
  Also, this approach is the suggested one in the android developer documentation: <a href="http://developer.android.com/guide/topics/resources/more-resources.html#Id" rel="nofollow noreferrer">http://developer.android.com/guide/topics/resources/more-resources.html#Id</a></p>
  
  <p>Lastly, I have also read this: <a href="https://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view">What is the main purpose of setTag() getTag() methods of View?</a> which explains the purpose of Tags and the use of setTag()/getTag() methods. This may be the best approach I've found so far for what I need. However, it surely introduces more complicated, unsupportable code. In any case, the right way, I think, has to be connected with ids, whose purpose is to uniquely identify views, rather than tags.</p>