Defining custom-checkbox in android

冷暖自知 提交于 2019-11-28 07:48:21

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

For your requirement I prefer you to use CheckedTextView instead of CheckBox.Here is the code what you wanted.

 <CheckedTextView
    android:id="@+id/ctv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Mon" 
    android:textSize="22sp"
    android:layout_margin="30dp"
    android:checked="true"
    android:background="@drawable/chk_indicator"
    android:padding="15dp"
   />    

create 3 xml ( chk_indicator.xml , chk_bg.xml , chk_pressed_bg.xml )in drawable folder

chk_indicator.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/chk_pressed_bg" />

<item android:drawable="@drawable/chk_bg" />

</selector>

chk_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle" >

         <solid android:color="#ffffff"/>

         <stroke android:width="3dp"
             android:color="#ababab"/>

        <corners android:radius="10dp"/>
    </shape>
    </item>
</layer-list>

chk_pressed_bg.xml

 <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
    <shape android:shape="rectangle" >

         <solid android:color="#ffffff"/>

         <stroke android:width="3dp"
             android:color="#5e9eff"/>

        <corners android:radius="10dp"/>
    </shape>
    </item>
 </layer-list>

output:

image http://i41.tinypic.com/2dawois.png

set onClick event on CheckedTextView

 ((CheckedTextView)findViewById(R.id.ctv)).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            boolean isChecked = ((CheckedTextView)findViewById(R.id.ctv)).isChecked();

            if(isChecked)
              ((CheckedTextView)findViewById(R.id.ctv)).setChecked(false);
            else
              ((CheckedTextView)findViewById(R.id.ctv)).setChecked(true);




        }
    });
<CheckBox
    android:id="@+id/DinnerRG_ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="2dp"
    android:background="@drawable/yourbuttonbackground"
    android:button="@android:color/transparent"
    android:gravity="center_horizontal"
    android:padding="5dp"
    android:text="Wed"
    android:textSize="15dp" />

Try like this.

Tony

Adding to zohreh's answer, you could get the checkbox with text only and get rid of the checkbox icon as shown below,

For this, set the button drawable to null in the constructor of the custom checkbox class, as shown below,

public MyCheckBox(Context context, AttributeSet attrs) {
    super(context, attrs);
    // Don't show the checkbox.
    setButtonDrawable(null);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!