Java Code Examples for android.widget.CheckBox#setBackgroundResource()

The following examples show how to use android.widget.CheckBox#setBackgroundResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AccountAdapter.java    From AppCompat-Extension-Library with Apache License 2.0 5 votes vote down vote up
private static void setupCheckBox(CheckBox checkBox) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        checkBox.setButtonDrawable(R.drawable.btn_checkbox_circle);
        checkBox.setBackgroundResource(R.drawable.btn_checkbox_circle_background);
    } else {
        Context context = checkBox.getContext();
        AppCompatDrawableManager dm = AppCompatDrawableManager.get();

        StateListDrawable button = new StateListDrawable();
        button.addState(new int[]{android.R.attr.state_checked},
                dm.getDrawable(context, R.drawable.ic_checkbox_circle_checked));
        button.addState(new int[]{},
                dm.getDrawable(context, R.drawable.ic_checkbox_circle_unchecked));
        ColorStateList buttonTint = new ColorStateList(new int[][]{ // states
                new int[]{android.R.attr.state_checked},
                new int[]{} // state_default
        }, new int[]{ // colors
                ThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated),
                ThemeUtils.getThemeAttrColor(context, R.attr.colorControlNormal)
        });
        Drawable buttonCompat = DrawableCompat.wrap(button);
        DrawableCompat.setTintList(buttonCompat, buttonTint);
        checkBox.setButtonDrawable(buttonCompat);

        ShapeDrawable background = new ShapeDrawable(new OvalShape());
        int backgroundTint = ThemeUtils.getThemeAttrColor(context, android.R.attr.colorBackground);
        Drawable backgroundCompat = DrawableCompat.wrap(background);
        DrawableCompat.setTint(backgroundCompat, backgroundTint);
        ViewCompatUtils.setBackground(checkBox, backgroundCompat);
    }
}
 
Example 2
Source File: XLHRatingBar.java    From fangzhuishushenqi with Apache License 2.0 4 votes vote down vote up
private void initView() {
    removeAllViews();
    for (int i = 0; i < countNum; i++) {
        CheckBox cb = new CheckBox(getContext());
        LayoutParams layoutParams;
        if (widthAndHeight == 0) {
            layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        } else {
            layoutParams = new LayoutParams((int) widthAndHeight, (int) widthAndHeight);
        }
        if (differentSize && countNum % 2 != 0) {
            Log.e("xxx", layoutParams.width + "");
            int index = i;
            if (index > countNum / 2) {
                index = countNum - 1 - index;
            }
            float scale = (index + 1) / (float) (countNum / 2 + 1);
            layoutParams.width = (int) (layoutParams.width * scale);
            layoutParams.height = layoutParams.width;
        }
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        if (i != 0 && i != countNum - 1) {
            layoutParams.leftMargin = (int) dividerWidth;
            layoutParams.rightMargin = (int) dividerWidth;
        } else if (i == 0) {
            layoutParams.rightMargin = (int) dividerWidth;
        } else if (i == countNum - 1) {
            layoutParams.leftMargin = (int) dividerWidth;
        }
        addView(cb, layoutParams);
        cb.setButtonDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        if (stateResId == -1) {
            stateResId = R.drawable.book_review_rating_bar_selector;
        }
        cb.setBackgroundResource(stateResId);
        if (i + 1 <= countSelected) {
            cb.setChecked(true);
        }
        cb.setEnabled(canEdit);
        cb.setOnClickListener(new MyClickListener(i));
    }

}
 
Example 3
Source File: XLHRatingBar.java    From BookReader with Apache License 2.0 4 votes vote down vote up
private void initView() {
    removeAllViews();
    for (int i = 0; i < countNum; i++) {
        CheckBox cb = new CheckBox(getContext());
        LayoutParams layoutParams;
        if (widthAndHeight == 0) {
            layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        } else {
            layoutParams = new LayoutParams((int) widthAndHeight, (int) widthAndHeight);
        }
        if (differentSize && countNum % 2 != 0) {
            Log.e("xxx", layoutParams.width + "");
            int index = i;
            if (index > countNum / 2) {
                index = countNum - 1 - index;
            }
            float scale = (index + 1) / (float) (countNum / 2 + 1);
            layoutParams.width = (int) (layoutParams.width * scale);
            layoutParams.height = layoutParams.width;
        }
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        if (i != 0 && i != countNum - 1) {
            layoutParams.leftMargin = (int) dividerWidth;
            layoutParams.rightMargin = (int) dividerWidth;
        } else if (i == 0) {
            layoutParams.rightMargin = (int) dividerWidth;
        } else if (i == countNum - 1) {
            layoutParams.leftMargin = (int) dividerWidth;
        }
        addView(cb, layoutParams);
        cb.setButtonDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        if (stateResId == -1) {
            stateResId = R.drawable.book_review_rating_bar_selector;
        }
        cb.setBackgroundResource(stateResId);
        if (i + 1 <= countSelected) {
            cb.setChecked(true);
        }
        cb.setEnabled(canEdit);
        cb.setOnClickListener(new MyClickListener(i));
    }

}