Cristian Maranca Cristian Maranca - 22 days ago 6
Android Question

Need checkbox to allow button to be pressed - IF not working

I made a button to open a date and time picker, but a CheckBox must be checked in order to activate my button. Without the checkbox, it works fine, with the chekbox checked or unchecked, it's not opening the date picker. Can you tell me why?

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

pick_date = (Button) findViewById(R.id.pick_date);
tv_result = (TextView) findViewById(R.id.tvdate_result);
CheckBox fixed_date_flag = (CheckBox) findViewById(R.id.fixed_date);
final boolean checked = (fixed_date_flag).isChecked();

if(checked){
pick_date.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(NewActivity.this, NewActivity.this, year, month, day);
datePickerDialog.show();
}
});}
}

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
yearFinal = year;
monthFinal = month + 1;
dayFinal = dayOfMonth;

Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);

TimePickerDialog timePickerDialog = new TimePickerDialog(NewActivity.this, NewActivity.this, hour, minute, DateFormat.is24HourFormat(this));
timePickerDialog.show();

}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
hourFinal = hourOfDay;
minuteFinal = minute;
tv_result.setText(
"year: " + yearFinal + "\n" +
"month: " + monthFinal + "\n" +
"day: " + dayFinal + "\n" +
"hour: " + hourFinal + "\n" +
"minute: " + minuteFinal);
}


}

Answer

your problem is very simple:

When creating the activity, your are checking if the checkbox is checked. If this value is true (but is not true) you add a click listener to the datePicker. So, the picker does not have a click listener! This means that when you "tap" the picker, it has no listener added, even if the checkbox is checked.

You must move the if statement inside the onClick method, like this:

EDITED:

pick_date.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View view)
    {
        if((fixed_date_flag).isChecked()){
            Calendar c = Calendar.getInstance();
            year = c.get(Calendar.YEAR);
            month = c.get(Calendar.MONTH);
            day = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog datePickerDialog = new DatePickerDialog(NewActivity.this, NewActivity.this, year, month, day);
            datePickerDialog.show();
         }
    }
});

Nevertheless, I think is a much better option to create a separated cass that implements OnClickListener interface, so in the constructor you can initialize the datepicker, instanciated as a privated variable of this new class, and on the onClick method you only call to .show() method (if the checkbox is checked, obbiusly)

Hope it helps!