jigar
jigar

Reputation: 1591

imageView not changed when clicked in android

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);
            }

Upvotes: 0

Views: 776

Answers (4)

Rushabh Patel
Rushabh Patel

Reputation: 3080

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.

Upvotes: 1

Lohit
Lohit

Reputation: 67

    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.

Upvotes: 0

dors
dors

Reputation: 5892

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

Upvotes: 0

Akhilesh Mani
Akhilesh Mani

Reputation: 3552

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>

Upvotes: 2

Related Questions