0

This question is more about best practices.

I'm working on some code that creates a UI from a dynamic XML file. It took me quite a while to discover that Dynamic Views have an ID of -1. Which means if you want to layout Dynamic Views in relation to other Dynamic Views you need to give them IDs on creation.

So I did that, but I don't really like the solution I came up with, and there's a chance that I could accidentally give the same ID to two elements.

Is there a way to let Android assign unique IDs to these dynamic views, or do I have to create some sort of ID tracking code myself?

  • Similar questions [here](http://stackoverflow.com/questions/8460680/how-can-i-assign-an-id-to-a-view-programmatically) or [here](http://stackoverflow.com/questions/6790623/programmatic-views-how-to-set-unique-ids). – gunar Oct 21 '13 at 09:40
  • Here are the answers you should refer to -: http://stackoverflow.com/questions/1714297/android-view-setidint-id-programmatically-how-to-avoid-id-conflicts – Abhishek Birdawade Oct 21 '13 at 09:40
  • I don't understand your problem, the id you give to views in xml are String you shoose. Android associate the String with int but you don't have to bother, it's mangaged automatically. If you want to give id based on some criteria you should ro it programatically – An-droid Oct 21 '13 at 09:40
  • @AbhishekBirdawade could u try programatically create layout views. It's easy one – Karthikeyan Karthik Oct 21 '13 at 09:49

1 Answers1

0

Taken from here

From API level 17, you can call

View.generateViewId()
Community
  • 1
  • 1
Mohsen Afshin
  • 12,711
  • 8
  • 63
  • 84