Skip to content

Commit 9c78636

Browse files
committed
Utils class for generating unique view ids.
1 parent 44bbb90 commit 9c78636

File tree

1 file changed

+42
-0
lines changed
  • library/src/com/wrapp/floatlabelededittext

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.wrapp.floatlabelededittext;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
5+
import android.annotation.SuppressLint;
6+
import android.os.Build;
7+
import android.view.View;
8+
9+
public class Utils {
10+
11+
private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
12+
13+
/**
14+
* Generate a value suitable for use in {@link #setId(int)}.
15+
* This value will not collide with ID values generated at build time by aapt for R.id.
16+
*
17+
* @return a generated ID value
18+
*/
19+
private static int generateViewId() {
20+
for (;;) {
21+
final int result = sNextGeneratedId.get();
22+
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
23+
int newValue = result + 1;
24+
if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
25+
if (sNextGeneratedId.compareAndSet(result, newValue)) {
26+
return result;
27+
}
28+
}
29+
}
30+
31+
@SuppressLint("NewApi")
32+
public static int generateId() {
33+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
34+
35+
return generateViewId();
36+
} else {
37+
38+
return View.generateViewId();
39+
}
40+
}
41+
42+
}

0 commit comments

Comments
 (0)