onClick in TimePickerDialog does not respond

30 views Asked by At

I want to have a ListView where I can click on the single items. A TimePickerDialog should show up and let the user pick a time and also confirm his choice. So I used setButton(BUTTON_POSITIVE, ...) for this. The dialog opens up as it should, but if I click on any of the two Buttons, the dialog will just dismiss without leaving any message except for:

sendCancelIfRunning:isInProgress=falsecallback=android.view.ViewRootImpl$$ExternalSyntheticLambda17@a929e50

The buttonclick Log.d(TAG, "onClick: Confirmed") is never logged and the debugger also never even enters the onClick() of the TimePickerDialog.

public class AddStopActivity extends Activity {
    [...]
    @Override
    public void onCreate(Bundle savedInstanceState) {

        [...]

        ListView listView = findViewById(R.id.listview);
        SearchView searchView = findViewById(R.id.addStopSearch);
        arrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.medium_text_list_item, busStops);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                final Calendar myCalender = Calendar.getInstance();
                int hour = myCalender.get(Calendar.HOUR_OF_DAY);
                int minute = myCalender.get(Calendar.MINUTE);
                TimePickerDialog.OnTimeSetListener timeListener = (v, hourOfDay, minuteOfDay) -> {
                    if (view.isShown()) {
                        myCalender.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        myCalender.set(Calendar.MINUTE, minuteOfDay);

                    }
                };
                TimePickerDialog timePickerDialog = new TimePickerDialog(AddStopActivity.this, android.R.style.Theme_Holo, timeListener, hour, minute, true);
                timePickerDialog.setTitle(R.string.dialog_select_time);
                timePickerDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

                int width = (int)(getResources().getDisplayMetrics().widthPixels*0.40);
                int height = (int)(getResources().getDisplayMetrics().heightPixels*0.40);
                timePickerDialog.getWindow().setLayout(width, height);
                timePickerDialog.setButton(BUTTON_POSITIVE, "Confirm", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d(TAG, "onClick: Confirmed");
                        dialog.dismiss();
                    }
                });
                timePickerDialog.show();
            }
        });
[...]
}

I tried logging and debugging the ButtonClick, but nothing showed any hints on what to do. I also tried calling setButton() after show(), but it also did nothing.

1

There are 1 answers

0
Ozgur Baykal On

You do not have to do this on the line where you set the button.

Instead, try doing the operations you want to do when the "Confirm" button is clicked in "OnTimeSetListener()".

//...
                TimePickerDialog.OnTimeSetListener timeListener = (v, hourOfDay, minuteOfDay) -> {
                    if (view.isShown()) {
                        myCalender.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        myCalender.set(Calendar.MINUTE, minuteOfDay);

                    }
                    Log.d(TAG, "Time selected: " + hourOfDay + ":" + minuteOfDay);

                };

//...

                timePickerDialog.setButton(BUTTON_POSITIVE, "Confirm", timePickerDialog);

//...

LOG:

Time selected: 18:10