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!
Asked
Active
Viewed 1,123 times
0
-
the question is do you really need the view id? – pskink Sep 29 '14 at 11:41
-
Read this http://stackoverflow.com/q/1714297/2191256 – vilpe89 Sep 29 '14 at 11:43
-
I think your answer is here.... [adding TextViews programmatically][1] [1]: http://stackoverflow.com/questions/1714297/android-view-setidint-id-programmatically-how-to-avoid-id-conflicts – Sudheesh Mohan Sep 29 '14 at 11:46
-
See this soluction http://stackoverflow.com/a/15442898 – QArea Sep 29 '14 at 14:16
3 Answers
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
-
Thanks. I found android automatically generated an id for me. No need to worry. – Winlandiano Sep 29 '14 at 11:52
-
ok nice, but above method is for generating your own id and to use it as your own wish like as a count of anything. – Prince Sep 29 '14 at 12:16
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