Is there an easy way to use a custom image for a checkbox? I'm looking to duplicate the "starred" behavior of gmail. So I want to have a checkbox that, when checked, is a filled in star. And when unchecked is an empty star. Do I have to use an imageview and do my own logic myself?
Checkboxes being children of Button you can just give your checkbox a background image with several states as described here, under "Button style":
...and exemplified here:
android:button
solution below is much better than using background attribute ! –
Fin width
and height
to the bitmap in the selector once you specify android:button
? –
Moslem button
attribute, it will not scale a background image. If you use a background
attribute instead, and set android:button=@null
, your background will scale to the size of the checkbox –
Ambrogio Create a drawable checkbox selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/checkbox"
android:state_checked="false"/>
<item android:drawable="@drawable/checkboxselected"
android:state_checked="true"/>
<item android:drawable="@drawable/checkbox"/>
</selector>
Make sure your checkbox is like this android:button="@drawable/checkbox_selector"
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@drawable/checkbox_selector"
android:text="CheckBox"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/Black" />
<item android:drawable="@drawable/checkbox"/>
this is not needed and also takes precedence over check state as true. –
Nolasco Checkboxes being children of Button you can just give your checkbox a background image with several states as described here, under "Button style":
...and exemplified here:
android:button
solution below is much better than using background attribute ! –
Fin width
and height
to the bitmap in the selector once you specify android:button
? –
Moslem button
attribute, it will not scale a background image. If you use a background
attribute instead, and set android:button=@null
, your background will scale to the size of the checkbox –
Ambrogio Copy the btn_check.xml from android-sdk/platforms/android-#/data/res/drawable to your project's drawable folder and change the 'on' and 'off' image states to your custom images.
Then your xml will just need android:button="@drawable/btn_check"
<CheckBox
android:button="@drawable/btn_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
If you want to use different default android icons, you can use android:button="@android:drawable/..."
res/drawable/day_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/dayselectionunselected"
android:state_checked="false"/>
<item android:drawable="@drawable/daysselectionselected"
android:state_checked="true"/>
<item android:drawable="@drawable/dayselectionunselected"/>
</selector>
res/layout/my_layout.xml
<CheckBox
android:id="@+id/check"
android:layout_width="39dp"
android:layout_height="39dp"
android:background="@drawable/day_selector"
android:button="@null"
android:gravity="center"
android:text="S"
android:textColor="@color/black"
android:textSize="12sp" />
If you have Android open source code, you can find the styles definition under:
src/frameworks/base/core/res/res/values
<style name="Widget.CompoundButton.CheckBox">
<item name="android:background">
@android:drawable/btn_check_label_background
</item>
<item name="android:button">
?android:attr/listChoiceIndicatorMultiple
</item>
</style>
Based on the Enselic and Rahul answers.
It works for me (before and after API 21):
<CheckBox
android:id="@+id/checkbox"
android:layout_width="40dp"
android:layout_height="40dp"
android:text=""
android:gravity="center"
android:background="@drawable/checkbox_selector"
android:button="@null"
app:buttonCompat="@null" />
Try it -
package com;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
public class CheckBoxImageView extends ImageView implements View.OnClickListener {
boolean checked;
int defImageRes;
int checkedImageRes;
OnCheckedChangeListener onCheckedChangeListener;
public CheckBoxImageView(Context context, AttributeSet attr, int defStyle) {
super(context, attr, defStyle);
init(attr, defStyle);
}
public CheckBoxImageView(Context context, AttributeSet attr) {
super(context, attr);
init(attr, -1);
}
public CheckBoxImageView(Context context) {
super(context);
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
setImageResource(checked ? checkedImageRes : defImageRes);
}
private void init(AttributeSet attributeSet, int defStyle) {
TypedArray a = null;
if (defStyle != -1)
a = getContext().obtainStyledAttributes(attributeSet, R.styleable.CheckBoxImageView, defStyle, 0);
else
a = getContext().obtainStyledAttributes(attributeSet, R.styleable.CheckBoxImageView);
defImageRes = a.getResourceId(0, 0);
checkedImageRes = a.getResourceId(1, 0);
checked = a.getBoolean(2, false);
a.recycle();
setImageResource(checked ? checkedImageRes : defImageRes);
setOnClickListener(this);
}
@Override
public void onClick(View v) {
checked = !checked;
setImageResource(checked ? checkedImageRes : defImageRes);
onCheckedChangeListener.onCheckedChanged(this, checked);
}
public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
this.onCheckedChangeListener = onCheckedChangeListener;
}
public static interface OnCheckedChangeListener {
void onCheckedChanged(View buttonView, boolean isChecked);
}
}
Add this attrib -
<declare-styleable name="CheckBoxImageView">
<attr name="default_img" format="integer"/>
<attr name="checked_img" format="integer"/>
<attr name="checked" format="boolean"/>
</declare-styleable>
Use like -
<com.adonta.ziva.consumer.wrapper.CheckBoxImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/checkBox"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="true"
android:padding="5dp"
app:checked_img="@drawable/check_box_checked"
app:default_img="@drawable/check_box" />
It will fix all your porblems.
onSaveInstanceState()
and onRestoreInstanceState()
methods, I think the checked state would be lost on rotation –
Galatea Another option is to use a ToggleButton with null background and a custom button.
Bellow an example that includes a selector to the text color as well.
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/toggle_selector"
android:background="@null"
android:paddingLeft="10dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textColor="@drawable/toggle_text"
android:textOn="My on state"
android:textOff="My off state" />
toggle_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="@drawable/state_on" />
<item
android:drawable="@drawable/state_off" />
</selector>
toggle_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:color="@color/app_color" />
<item
android:color="@android:color/darker_gray" />
</selector>
If you are using custom adapters than android:focusable="false"
and android:focusableInTouchMode="false"
are nessesury to make list items clickable while using checkbox.
<CheckBox
android:id="@+id/checkbox_fav"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/checkbox_layout"/>
In drawable>checkbox_layout.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/uncked_checkbox"
android:state_checked="false"/>
<item android:drawable="@drawable/selected_checkbox"
android:state_checked="true"/>
<item android:drawable="@drawable/uncked_checkbox"/>
</selector>
If you use androidx.appcompat:appcompat and want a custom drawable (of type selector
with android:state_checked
) to work on old platform versions in addition to new platform versions, you need to use
<CheckBox
app:buttonCompat="@drawable/..."
instead of
<CheckBox
android:button="@drawable/..."
Adding custom drawable in the android:button did not work for me in Material Checkbox version-1.3.0
. I had to set android:drawable="@drawable/checkbox_selector"
instead and also set android:button="@null"
. You can also add android:drawablePadding
to make it look good. However, this makes the entire checkbox clickable (along with the text).
All you need two drawable one for checked state and other for unchecked or default state. You can then create a background drawable selector(xml) and apply it as background to your checkbox. As mentioned below
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="@drawable/ic_checkbox_checked_24" />
<item
android:state_checked="false"
android:drawable="@drawable/ic_checkbox_unchecked_24" />
<item
android:drawable="@drawable/ic_checkbox_unchecked_24"/>
</selector>
No your checkbox in xml should look like this.
<CheckBox
android:id="@+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:background="@drawable/checkbox_background_drawable"/>
Make sure you have maked the button attribute of checkbox to null.
© 2022 - 2024 — McMap. All rights reserved.