Reputation: 97
I have 3 check boxes..I have added click listener for each check boxes..based on the check box click i had set the values inside click listener...After adding the check box click,there will be possibility to uncheck it while updating the check box...
While clicking the check box i am setting isSelected =1...Like the way i need to set isSelected =0 while it is unchecked...How is it possible..Please help me to find out
This is my check box click listener1
checkBox_onEventDay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
response = 1;
try {
//Here we are getting the date from btn_Date(date picker)
//date and time format changed here
String eventDate = btn_Date.getText().toString();
DateFormat date = new SimpleDateFormat("dd-M-yyyy");
Date date1 = date.parse(eventDate);
DateFormat convertDate = new SimpleDateFormat(" dd.MM.yyyy");
eventDate = convertDate.format(date1);
eventMO.setEventDate(eventDate);
//here we are setting event date as reminder date..
//Because it is on event day checkbox
reminderDate = eventDate;
eventReminderDaysDetails(response, reminderDate);
Toast.makeText(OccasionActivity.this,
"Checked", Toast.LENGTH_LONG).show();
} catch (ParseException pExp) {
pExp.printStackTrace();
}
}
}
});
Upvotes: 0
Views: 168
Reputation: 3692
I am guessing you have an unchecked check box in the beginning. If not, you can initialize the value of isSelected
accordingly. You need to handle onClick
like this:
final boolean isSelected = false;
checkBox_onEventDay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isSelected) {
isSelected = false;
// now check box is unchecked
} else {
isSelected = true;
// now the checkbox is checked
}
}
});
Upvotes: 2
Reputation: 5451
You can use below code :
if(cb.isChecked()) {
isSelected = 1 ;
Log.e("Checkbox is checked" , ""+isSelected);
} else {
isSelected = 0 ;
Log.e("Checkbox is unchecked" , ""+isSelected);
}
As simple as that.
Upvotes: 1