I have implemented a TimePickerFragment in a fragment. The time picker opens and I can select the time. But, the TextView value isn't updating even after selecting a time through TimePickerDialog. There are other function calls as well inside onTimeSet method which are also not working.
I've followed this tutorial.
Here' the code for TimePickerFragment:
public class TimerPickerFragment extends DialogFragment {
@NonNull
@NotNull
@Override
public Dialog onCreateDialog(@Nullable @org.jetbrains.annotations.Nullable 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) getTargetFragment(), hour, minute, DateFormat.is24HourFormat(getActivity()));
}
}
And the Fragment inside which I'm calling the TimePickerFragment is as follows:
public class Fragment_alarm extends Fragment implements TimePickerDialog.OnTimeSetListener {
private NotificationHelper mNotificationHelper;
private TextView mTextView;
@Nullable
@org.jetbrains.annotations.Nullable
@Override
public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Set Alarm");
View view = inflater.inflate(R.layout.fragment_alarm, container, false);
Button buttonTimePicker = view.findViewById(R.id.timeButton);
mTextView = view.findViewById(R.id.time_tv);
buttonTimePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment timePicker = new TimerPickerFragment();
timePicker.show(getActivity().getSupportFragmentManager(), "time picker");
}
});
Button buttonCancelAlarm = view.findViewById(R.id.button_cancel);
buttonCancelAlarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancelAlarm();
}
});
return view;
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
updateTimeText(c);
startAlarm(c);
}
private void updateTimeText(Calendar c) {
String timeText = "Alarm set for: ";
timeText += DateFormat.getTimeInstance(DateFormat.SHORT).format(c.getTime());
mTextView.setText(timeText);
}
private void startAlarm(Calendar c) {
...
}
private void cancelAlarm() {
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getActivity(), AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1, intent, 0);
alarmManager.cancel(pendingIntent);
mTextView.setText("Alarm cancelled");
}
}
The cancelAlarm method works fine as expected. But the other two functions which are being called from onTimeSet aren't working.
Please help me resolve this issue. Thank you in advance.
This is the code of one of my alarm project. just trigger this method at onClickEvent of the button, Hope this work for you