generate View Id for sdk before jelly bean - Android User Interface

Android examples for User Interface:View Id

Description

generate View Id for sdk before jelly bean

Demo Code


//package com.java2s;
import android.os.Build;
import android.view.View;
import java.util.concurrent.atomic.AtomicInteger;

public class Main {
    private static AtomicInteger sNextGeneratedId = new AtomicInteger(1);
    private final static int MAX_VIEW_ID_VALUE = 0x00FFFFFF;

    public static int generateViewId() {
        final int version = android.os.Build.VERSION.SDK_INT;

        if (version < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            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 > MAX_VIEW_ID_VALUE) {
                    newValue = 1; // Roll over to 1, not 0.
                }/*  w  ww.j a v  a2 s.  co  m*/
                if (sNextGeneratedId.compareAndSet(result, newValue)) {
                    return result;
                }
            }
        } else {
            return View.generateViewId();
        }
    }
}

Related Tutorials