0

I'm trying to create scheduled notifications on my Android Application using Time Picker and Date Picker Dialog to set time and date.

When I run the apk,

Date Picker works . Set Reminder button creates a notification on the given date. However, Time Picker button opens a small fragment dialog box with no content or view(like below). So I guess Time Picker Dialog box is not called somehow.

enter image description here This is the screenshot after clicking SAVE button. (Save button actually the button to open Time Picker Dialog. Too lazy to change names)

I have pasted my whole code here. Please Solve the following doubts :

1. Why is the Time Picker Dialog not displaying? Please tell me what I missed.

2. I'm having my doubt on my Time Picker Callbacks. Are the selected values of time returning to setAlarm() ?

3. Is there anything I should add to my Manifest file?

    public class ReminderFragment extends FragmentActivity {
    // declarations here
       private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                pHour = hourOfDay;
                pMinute = minute;
            }
        };

    private DatePickerDialog.OnDateSetListener ondate =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
                pYear = year;
                pMonth = monthOfYear;
                pDay = dayOfMonth;

    }
};

OnCreate method :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_set_reminder);
    /** Listener for click event of the button */

    findViewById(R.id.dialog).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            showDatePicker();
        }
    });

    findViewById(R.id.set_button).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            showTimePickerDialog(v)   ;         
        }
    });

    findViewById(R.id.btnSetReminder).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            setAlarm();
        }
    });


}

showTimePickerDialog method :

public Dialog showTimePickerDialog(View v) {
    DialogFragment newFragment = new DialogFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");

    // Use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    // Create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(this, mTimeSetListener, hour, minute,true);
}

showDatePicker method :

private void showDatePicker() {
    DatePickerFragment date = new DatePickerFragment();
    /**
     * Set Up Current Date Into dialog
     */
    Calendar calender = Calendar.getInstance();
    Bundle args = new Bundle();
    args.putInt("year", calender.get(Calendar.YEAR));
    args.putInt("month", calender.get(Calendar.MONTH));
    args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
    date.setArguments(args);
    /**
     * Set Call back to capture selected date
     */
    date.setCallBack(ondate);
    date.show(getSupportFragmentManager(), "Date Picker");
}

setAlarm method :

public void setAlarm() {
          calcal = new GregorianCalendar();
          //TODO -- CHANGE TIME HERE
          calcal.set(pYear, pMonth, pDay, pHour, pMinute); //change minute and seconds before compiling


          Intent alertIntent = new Intent(ReminderFragment.this, AlertReceiver.class);

          AlarmManager alarmManager = (AlarmManager)
                  getSystemService(Context.ALARM_SERVICE);
          alarmManager.set(AlarmManager.RTC_WAKEUP, calcal.getTimeInMillis(),
                  PendingIntent.getBroadcast(ReminderFragment.this, 1, alertIntent,
                          PendingIntent.FLAG_UPDATE_CURRENT));


      }

}

pblead26
  • 755
  • 1
  • 9
  • 30

1 Answers1

0

You are doing it in the oposite order.

You are creating the fragment and showing it, and after that you are creating the timepicker and returning it.

You must create the timepicker, put it in the fragment and then show the fragment.

juanlugm
  • 155
  • 1
  • 11
  • You can easily find many examples of TimePickers in fragments. For example here. You should look for DialogFragments. http://stackoverflow.com/questions/6668619/how-to-create-datepicker-and-timepicker-dialogs-in-fragment-class – juanlugm Dec 15 '15 at 22:41