Rajkumar Reddy
Rajkumar Reddy

Reputation: 2357

How to create single check box event for all check boxes in android?

I'm new to android it is possible in .net, But i want to build this logic in android is it possible to do that please help me, i know only creating check box event for each check box instead of that i want to create single event for all Check boxes please help me.

Vb.net

private sub Chkbox1_Checkchanged(byval sen as object,byval e as system.eventargs) handles Chkbox1.checked,chkbox2.checked,chkbox3.checked
    blnvalue=true
end sub

android

  chkbox1= (CheckBo1) findViewById(R.id.chkbox1);
  rbtn1.setOnClickListener(new View.OnClickListener()
  {         
      public void onClick(View v) 
      {             
              blnvalue=true;
      }
   });

Upvotes: 0

Views: 335

Answers (4)

Kartik Bhatt
Kartik Bhatt

Reputation: 924

You can also use onChackChanged Event & OnClickEvent for checkbox.

chkbox1= (CheckBo1) findViewById(R.id.chkbox1);
chkbox2= (CheckBo1) findViewById(R.id.chkbox2);


chkbox1.setOnClickListener(this);
chkbox1.setOnCheckedChangeListener(this);

chkbox2.setOnClickListener(this);
chkbox2.setOnCheckedChangeListener(this);

public void onClick(View v) 
{
   if(v.getId() == R.id.chkbox2)
          // your code
}

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    if (buttonView.getId() == activity.getCheckRefresh()) {
        activity.setRefreshRate("1");
        activity.setEnable(isChecked);
        return;
    }
}

Upvotes: 1

nicholas.hauschild
nicholas.hauschild

Reputation: 42834

Assuming you are in an Activity, you can make your Activity implement View.OnClickListener, and then use this for each View within your Activity, even ones that aren't CheckBox.

public class MyActivity extends Activity implements View.OnClickListener {

    //declare checkboxes

    public void onCreate(Bundle something) {
        //setup stuff and get views

        rbtn1.setOnClickListner(this);
        rbtn2.setOnClickListner(this);
        rbtn3.setOnClickListner(this);
        rbtn4.setOnClickListner(this);
    }

    public void onClick(View v) {
       if(v == rbtn1) {
           //do stuff for checkbox1
       } else if(v == rbtn2) {
           //do stuff for checkbox2
       } else if(v == rbtn3) {
           //do stuff for checkbox3
       } else if(v == rbtn4) {
           //do stuff for checkbox4
       }
    }
}

Upvotes: 1

Nirav
Nirav

Reputation: 5760

implements onCheckChangedEvent for for all checkBox.

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (buttonView.getId() == activity.getCheckRefresh()) {
            activity.setRefreshRate("1");
            activity.setEnable(isChecked);
            return;
        }
}

Upvotes: 2

ngesh
ngesh

Reputation: 13501

u can do this.. here click is an inner class.. and create an instance of it n pass it to every setOnClickListener(click)

class click implements OnClickListener
{
 public void onClick(View v) 
{
// your code
}

}

Upvotes: 0

Related Questions