Implementing OnCheckedChanged in Java

777 views Asked by At

Basically in one fragment, I have 3 toggle buttons, therefore I decided that my interface should implement OnCheckedChange. These buttons are : date button, time button and now button.

The work flow would be the following:

date is pressed, date image is changed to being active, check if time is pressed, check mark is active time is pressed, date image is changed to being active, check if date is pressed, check mark is active

now is pressed, date and time won't be active but check mark will. What I've done is the following:

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        switch (buttonView.getId()) {
        case R.id.select_date_button: {
            DialogFragment datePicker = new DatePickerFragment();
            datePicker.setTargetFragment(CheckinNow.this, 100);
            datePicker.show(getActivity().getSupportFragmentManager(), "datePicker");
            date_image.setBackgroundResource(R.drawable.btn_calendar_active);

            break;
        }

        case R.id.select_time_button: {
            DialogFragment timePicker = new TimePickerFragment();
            timePicker.setTargetFragment(CheckinNow.this, 100);
            timePicker.show(getActivity().getSupportFragmentManager(), "timePicker");
            time_image.setBackgroundResource(R.drawable.btn_time_active);

        }

        case R.id.now_button: {
            now_button.setBackgroundResource(R.drawable.btn_now_with_text_active);
            checkin_button_done.setBackgroundResource(R.drawable.btn_checkmark_active);
            time_image.setBackgroundResource(R.drawable.btn_time);
            date_image.setBackgroundResource(R.drawable.btn_calendar);

        }

        default:
            break;
    }

My question is how can I check when date is pressed and time as well or the other way around?

I can, of course, set an OnCheckedChanged for each button individually, but I'd prefer this solution if possible.

1

There are 1 answers

0
NSimon On BEST ANSWER

How about storing your Buttons with

ToggleButton dateButton = (ToggleButton) findViewById(R.id.select_date_button);

Then if you need to check if one button is checked or not, just call

if (dateButton.isChecked()) {
//Do some stuff
}

Hope this helps!