Question

I need to implement both custom TimePicker and DatePicker DialogPreferences in my project. First DatePicker Dialog should be called and then TimePicker. I implemented them both to my project with the help of this and this tutorials.

I think that I should call TimePreference in the onDialogClosed method of the DatePreference. I do it like that:

    @Override
protected void onDialogClosed(boolean shouldSave) {
    if (shouldSave && this.changedValueCanBeNull != null) {
      setTheDate(this.changedValueCanBeNull);
      this.changedValueCanBeNull = null;
      Log.i("curr context", ctx.getClass().toString());
      try{

          new TimePreference(ctx,attrs).getDialog().show();
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
    }
}

But I get an NullPointerException. Question is how can I call my custom TimePreference dialog programatically right after the DatePreference work and set both values to the DatePreference. Or I should create different custom ListPreference which should implement both of that items?

Was it helpful?

Solution

I created custom View which looks like ListPreference and implemented this solution.

  private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour) {
              mHour = hourOfDay;
              mMinute = minuteOfHour;
              updateDisplay();

            }
          };

             DatePickerDialog.OnDateSetListener mDateSetListener =
                     new DatePickerDialog.OnDateSetListener() {
                     public void onDateSet(DatePicker view, int year, int monthOfYear,
                                     int dayOfMonth) {
                             mYear = year;
                             mMonth = monthOfYear;
                             mDay = dayOfMonth;

                             updateDisplay();
                     }
             };


@Override
        protected Dialog onCreateDialog(int id) {
                switch (id) {
                case DATE_DIALOG_ID:
                        return new DatePickerDialog(this,
                                mDateSetListener,
                                mYear, mMonth, mDay);

                case TIME_DIALOG_ID:
                    return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,
                        false);
                }
                return null;
        }
        protected void onPrepareDialog(int id, Dialog dialog) {
                switch (id) {
                case DATE_DIALOG_ID:
                        ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
                        break;}
                }
        private void updateDisplay() {
            String minutes = null;
            if (mMinute>9)
                minutes = Integer.toString(mMinute);
            else 
                minutes = "0"+Integer.toString(mMinute);
            if (mMonth<10)
            deadlineCus.setSummary(
                    (new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mYear).append("-").append("0")
                    .append(mMonth + 1).append("-")
                    .append(mDay)
                    .append(" ").append(mHour).append(":").append(minutes)).toString()
                    );
            else 
                deadlineCus.setSummary(
                        (new StringBuilder()
                        // Month is 0 based so add 1
                        .append(mYear).append("-")
                        .append(mMonth + 1).append("-")
                        .append(mDay)
                        .append(" ").append(mHour).append(":").append(minutes)).toString()
                        );
            showDialog(TIME_DIALOG_ID);

    }

in onCreate() method:

 DatePickerDialog.OnDateSetListener mDateSetListener =
                    new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                                    int dayOfMonth) {
                            mYear = year;
                            mMonth = monthOfYear;
                            mDay = dayOfMonth;

                            updateDisplay();
                    }


            };
            TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour) {
                  mHour = hourOfDay;
                  mMinute = minuteOfHour;
                  updateDisplay();

                }
              };

  deadlineCus.setOnTouchListener(new OnTouchListener()
      {

        public boolean onTouch(View arg0, MotionEvent arg1) 

        {
              showDialog(DATE_DIALOG_ID);
              return false;
        }

      });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top