title: how to generate unique ID's for layouts in android?
body: <p>I have an app that creates controls programmatically according to information coming from a database.</p>
  
  <p>In the application I need to reference a dynamically created Layout, so that I need to assign it an ID.</p>
  
  <p>Currently, I have:</p>
  
  <pre><code>    final Random random = new Random();
      for (int i = 0; i &lt; count; i++) {
          TdcItem item = items[i];
  
          int id = item.getID();
          final int layoutId = random.nextInt(id * (id + 1));
  
          ....
      }
  </code></pre>
  
  <p>Where id variable is an ID coming from the database, which of course is unique in database table.</p>
  
  <p>The problem is that I am also creating buttons coming from another table, so, the ID's are unique from that table point of view, however, some of them are not unique in activity context.</p>
  
  <p>That cause that when I use findViewById trying to find a layout, a button is found instead, throwing an invalid cast exception.</p>
  
  <p>Is there a way to create unique ID's for the layouts?</p>
  
  <p>Thanks
  Jaime</p>