Skip to content

Instantly share code, notes, and snippets.

@hrules6872
Created April 9, 2015 13:49
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
A backport of generateViewId() to API 10+
public class CustomGenerateViewId {
private static final AtomicInteger nextGeneratedId = new AtomicInteger(1);
public static int customGenerateViewId() {
for (; ; ) {
final int result = nextGeneratedId.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 (nextGeneratedId.compareAndSet(result, newValue)) {
return result;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment