Use custom view
import android.content.Context; import android.util.AttributeSet; import android.widget.Switch; public class CustomSwitch extends Switch { private OnCheckedChangeListener mListener; public CustomSwitch(Context context) { super(context); } public CustomSwitch(Context context, AttributeSet attrs) { super(context, attrs); } public CustomSwitch(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public CustomSwitch(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
XML example
<com.your.package.CustomSwitch android:layout_width="wrap_content" android:layout_height="wrap_content"/>
Now the idea is to call the setCheckedProgrammatically method in code. setChecked is called by Android when users change the state of the compund button
Please note that I use a switch that extends the function button, you can use basically the same code for any other (checkbox, ...)
Bojan Kseneman Apr 15 '15 at 21:04 2015-04-15 21:04
source share