Reputation: 27
I've tried to implement a Time Picker which is opened up by a SwitchCompat, so when I enable the option, the Time Picker shows up, it works fine but if I press cancel, or back button or simply press outside of the time picker, the Switch stays at the ON position and I want it to get back to OFF when I don't choose the time in Time Picker. Thought that maybe onCancel method could help but it does not, or simply I made some mistake with implementing it, there's the related code:
public class Settings extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener, TimePickerDialog.OnDismissListener, TimePickerDialog.OnCancelListener {
private EditText textLastHour;
private KeyListener keyListener;
private SwitchCompat hourSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
textLastHour = findViewById(R.id.finalHourText);
textLastHour.setKeyListener(null);
textLastHour.setHint("Option not selected");
hourSwitch = findViewById(R.id.switch_hour);
hourSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
DialogFragment timePicker = new TimePickerFragment();
timePicker.show(getSupportFragmentManager(), "time picker");
} else {
textLastHour.setText("");
}
}
});
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
textLastHour = findViewById(R.id.finalHourText);
textLastHour.setText("Final alarm at: " + hourOfDay + ":" + minute);
}
@Override
public void onCancel(DialogInterface dialog) {
hourSwitch.setChecked(false);
}
@Override
public void onDismiss(DialogInterface dialog) {
hourSwitch.setChecked(false);
}
}
I've tried both onDismiss
and onCancel
and neither have worked here, is there any solution to make the Switch go back to the original position if you cancel the Time Picker? Added the DatePicker
to tags as I feel like it may have the same solution as TimePicker
for that problem.
My TimePickerFragment
Class:
public class TimePickerFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, DateFormat.is24HourFormat(getActivity()));
}
}
Upvotes: 0
Views: 362
Reputation: 392
It's actually simpler than I thought all you need to do is to add a dismiss listener and change your ui/logic accordingly
TimePickerDialog dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
}, 10, 10, true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Toast.makeText(MainActivity.this, "dismissed", Toast.LENGTH_LONG).show();
}
});
dialog.show();
You can use the standard TimePickerDialog
Edit: Put everything to onCreate()
except for dialog.show()
, dialog.show()
goes into the ifChecked in the Switch
Upvotes: 1
Reputation: 530
As I see in your code you haven't defined any listener in your TimePickerFragment class. you can add following code in your TimePickerFragment :
public class TimePickerFragment(Context context) extends DialogFragment{
interface OnCompleteListener {
abstract void onComplete(boolean suceeded);
}
OnCompleteListener listener;
/**
* your other lines of code
**/
}
Don't forget to set context argument for your class !
Then override onAttach function and inside that implement your listener like this :
try {
this.listener = (OnCompleteListener) context;
} catch (ClassCastException e) {
throw ClassCastException(activity.toString() + " must implement OnCompleteListener");
}
After this in your override onDismiss() and set listener.onComplete(false);
inside that. And whenever the TimePickerFragment wanted to finish successfully just set listener.onComplete(true);
Now we shall back to MainActivity. First of all make MainActivity implement TimePickerFragment.OnCompleteListener
.
Now you need to override the abstract function of your OnCompleteListener
. Simply if the argument was false it means that dialog dismissed unsuccessfully, so with a simple if condition set your Switch
to off programmatically.
Hope I could help.
Upvotes: 0