0

When I dynamically generated a View, like textView, how can I automatically generate an Id for this textView? I know I can use the method setId, but I'm afraid that this Id i manually set would possibly conflict with some other Views' Id. Just in case! When I set the layout parameters in relative layout, the id for all views seems necessary. Is there any method i can use to fix this? Thanks!

3 Answers3

0

First ask yourself if the id is really necessary, if it is and you are worrying about possible conflicts there are previous questions similar to yours.

Take a look: Android: View.setID(int id) programmatically - how to avoid ID conflicts?

Community
  • 1
  • 1
AndroidEnthusiast
  • 6,557
  • 10
  • 42
  • 56
0

to set id:

int count =0;

your_tv.setId(count);

to retrieve from that id:

your_tv.getId();

// here count can be any positive integer, it won't conflict with other view's ids.

Prince
  • 496
  • 3
  • 12
0

You can use the method your_view.generateViewId() from API 17 which will give you an id unique for your view.

Shainu Thomas
  • 360
  • 3
  • 12