Google - How to generate dynamic id in android, Dynamically generate View ID in android
How to generate dynamic id in android
You can use below code to create or generate dynamic ids or generate id dynamically for views without any conflicts.private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1); @SuppressLint('NewApi') public static int generateViewId() { if (Build.VERSION.SDK_INT < 17) { for (;;) { final int result = sNextGeneratedId.get(); // aapt-generated IDs have the high byte nonzero; clamp to the range under that. int newValue = result + 1; if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0. if (sNextGeneratedId.compareAndSet(result, newValue)) { return result; } } } else { return View.generateViewId(); } }
In this function if API level is greater than 17 then the function will use the inbuild generateViewId() function to create the id, else it will generate ids smaller than 0x00FFFFFF as ids greater than this value is assigned for the default android resources.
The topic on Google - How to generate dynamic id in android is posted by - Guru
Hope you have enjoyed, Google - How to generate dynamic id in androidThanks for your time