0

i've made a simple custom checkbox program in android,in that i have tken two images for "difault" and "checked" state as per user action i want to change that images..i have tried the following code which is not working,

enter image description here

my code is:

final ImageView chekbx =(ImageView)dialog.findViewById(R.id.chk_login);
            if(chekbx.isSelected()){
                System.out.println("checkbox check");
                chekbx.setBackgroundResource(R.drawable.checkbox_ticked);
            }else{
                chekbx.setBackgroundResource(R.drawable.checkbox);
            }
jigar
  • 1,571
  • 6
  • 23
  • 46

4 Answers4

2

Use selector for this purpose .

This is your checkboc:

          <CheckBox
            android:id="@+id/remb_ckh_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/check_box_selector" />

And its selector :

<?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/checkbox_selected" />

<item android:state_checked="false" android:drawable="@drawable/checkbox_unselected" />

</selector>
Akhilesh Mani
  • 3,502
  • 5
  • 28
  • 59
1

Try to use this code:

EDITED

final ImageView chekbx =(ImageView)dialog.findViewById(R.id.chk_login);
boolean flag =false; //TAKE AS A PUBLIC VAR
chekbx.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(flag)
        {
            chekbx.setBackgroundResource(R.drawable.checkbox);
            flag=false;
        }
        else
        {
            System.out.println("checkbox check");
            chekbx.setBackgroundResource(R.drawable.checkbox_ticked);
            flag = true;
        }
    }
});

Hope this will help you.

Rushabh Patel
  • 3,052
  • 4
  • 26
  • 58
  • Check out my edited answer...sorry i misunderstood checkbox instead of imageview. you can maintain one boolean that will tell you that imageview is checked or not. that is one logic you can go with. – Rushabh Patel Jun 28 '13 at 06:08
  • Yeah,,...Thats a like a true gujju bro...! thanx.....it worked for me....:) Thanx rushabh...:) – jigar Jun 28 '13 at 06:10
0

Android already has CheckBox view, you don't need to build one yourself.

Checkbox API: http://developer.android.com/reference/android/widget/CheckBox.html

This checkbox can also have custom images, using a selector: How to change default images of CheckBox

Community
  • 1
  • 1
dors
  • 5,802
  • 8
  • 45
  • 71
0
    void onClick Login(View v)
    {
        if(checkbx.isSelected(){
            chekbx.setBackgroundResource(R.drawable.checkbox_ticked);
            <Set your new database with login details and phone ID to remember>
            //check your database for login details here with registered details
            }
        else{
            chekbx.setBackgroundResource(R.drawable.checkbox);
            //check your database for login details here with registered details
        }
    }   

This logic should help you. Thanks.

Lohit
  • 67
  • 2
  • 9