Skip to content

Instantly share code, notes, and snippets.

@hrules6872
Created April 9, 2015 13:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrules6872/8492c931e345385823c0 to your computer and use it in GitHub Desktop.
Save hrules6872/8492c931e345385823c0 to your computer and use it in GitHub Desktop.
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