Custom checkbox image android
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:
Defining custom-checkbox in android
use this code
select.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffffff" >
</solid>
<stroke
android:width="2dp"
android:color="#ff0000" >
</stroke>
<corners android:radius="5dp" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
deselect.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffffff" >
</solid>
<stroke
android:width="2dp"
android:color="#000000" >
</stroke>
<corners android:radius="5dp" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
and custom checkbox
public class checkbox extends CheckBox{
public checkbox(Context context, AttributeSet attrs) {
super(context, attrs);
//setButtonDrawable(new StateListDrawable());
}
@Override
public void setChecked(boolean t){
if(t)
{
this.setBackgroundResource(R.drawable.select);
}
else
{
this.setBackgroundResource(R.drawable.deselect);
}
super.setChecked(t);
}
}
checkbox
<com.example.checkbox.checkbox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:checked="true"
android:text="checked" />
you can change color in select.xml and deselect.xml to thing that you want
Android replace checkbox style with custom drawable selector
Use below line of code, i think it will work
yourcheckbox.setButtonDrawable(yourdrawable);
Custom checkbox image - but can still see original?
The attribute you want to change is button
, not background
.
First, design your custom graphics for different button states. Use a state list drawable (docs) to associate the various images. For example:
<?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/checked" />
<item android:state_checked="false" android:drawable="@drawable/unchecked" />
</selector>
Save this into your drawable resources folder, something like my_checkbox.xml.
Now, use the button attribute on your checkbox layout definition:
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_alignParentRight="true"
android:button="@drawable/my_checkbox"
android:enabled="false" />
Customize Android Check Box
You need two drawables with desired look of the checkboxes and define drawable selector, like it's described here:
https://stackoverflow.com/a/7783892/1099716
There are plenty VectorDrawable icon generators as well as png generators which can be used as drawables. One of them can be found here: https://materialdesignicons.com/
There is also VectorDrawable compat for android 14+ https://github.com/wnafee/vector-compat
For more info regarding drawables usage take a look at the following tutorial http://www.vogella.com/tutorials/AndroidDrawables/article.html
Android - Custom multi-click Checkbox with Images
As stated in the PSS, I only had the idea for the solution when I was typing the question above. I've used an ImageButton that changes it's source when it's clicked. Here is the code:
ImageButton in xml:
<ImageButton
android:id="@+id/ibtnCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/checkbox_content_description"
android:src="@drawable/checkbox_unchecked"
android:background="@drawable/transparent_button_background" />
transparent_button_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@android:color/transparent" />
<item android:state_pressed="true" android:drawable="@android:color/transparent" />
<item android:drawable="@android:color/transparent" />
</selector>
Activity:
public class MainActivity extends ActionBarActivity {
private ImageButton cbButton;
private int status;
private int checkbox_images[] = {
R.drawable.checkbox_unchecked,
R.drawable.checkbox_checked,
R.drawable.checkbox_error,
R.drawable.checkbox_partly
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = 0;
addListenerToButton();
}
private void addListenerToButton(){
cbButton = (ImageButton) findViewById(R.id.ibtnCheckbox);
cbButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
switch(status){
// Unchecked -> Green checked
case 0:
// Green checked -> Red cross
case 1:
// Red cross -> Orange-yellow checked
case 2:
cbButton.setImageResource(checkbox_images[++status]);
break;
// Orange-yellow checked -> Unchecked
case 3:
cbButton.setImageResource(checkbox_images[status = 0]);
break;
// Default (just in case)
default:
status = 0;
cbButton.setImageResource(checkbox_images[status++]);
break;
}
}
});
}
...
}
Related Topics
How to Fire Onlistitemclick in Listactivity with Buttons in List
Gradle: Execution Failed for Task ':Processdebugmanifest'
How to Add Stacktrace or Debug Option When Building Android Studio Project
How to Lock/Unlock Screen Programmatically
"No Cached Version... Available for Offline Mode."
Firebase Firestore Variable Name Changed
Lock the Android Device Programmatically
Resources$Notfoundexception: Resource Is Not a Drawable (Color or Path)
How to Attach Javadoc or Sources to Jars in Libs Folder
Android Videoview Orientation Change with Buffered Video
Custom Toast on Android: a Simple Example
How to Make an Alert Dialog Fill 90% of Screen Size
Accessing Localhost:Port from Android Emulator
Taking Picture from Camera Without Preview
Notification Icon with the New Firebase Cloud Messaging System
Error:(1, 0) Plugin With Id 'Com.Android.Application' Not Found