18

I have a problem when I'm trying to set DatePickerDialog title permanently.

DatePickerFragment.java

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    DatePickerDialog dpd = new DatePickerDialog(getActivity(), this, year, month, day);
    dpd.setTitle("set date");
    return dpd;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    }
}

MainActivity.java

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.button1);

        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getSupportFragmentManager(), "datePicker");

            }
        });
    }
}

When I click button DatePickerDialog shows and dialog title is "set date" but when I change date, title contains selected date rather than "set date". How can I set this dialog title permanently?

Tested on API 8-10.

Thank you in advance and sorry for my English. Patrick

John
  • 339
  • 1
  • 3
  • 13

5 Answers5

31

How about extending DatePickerDialog and adding a setPermanentTitle method to store a permanent title that will be forced when date gets changed ?

public class MyDatePickerDialog extends DatePickerDialog {

    private CharSequence title;

    public MyDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
        super(context, callBack, year, monthOfYear, dayOfMonth);
    }

    public void setPermanentTitle(CharSequence title) {
        this.title = title;
        setTitle(title);
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        super.onDateChanged(view, year, month, day);
        setTitle(title);
    }
}

And then use the new setPermanentTitle method:

    MyDatePickerDialog dpd = new MyDatePickerDialog(this, null, 2012, 10, 10);
    dpd.setPermanentTitle("set date");
sdabet
  • 18,360
  • 11
  • 89
  • 158
  • 1
    Why not just overriding setTitle() ? – AsafK Dec 24 '13 at 11:58
  • Because `setTitle` is automatically called internally with the new selected date string when changing the date. – sdabet Dec 24 '13 at 12:52
  • But this is exactly what you are doing in onDateChanged() – AsafK Dec 24 '13 at 13:29
  • By default the title is set to the current date when changing the date. But we want to force our own title. That's why we need to call again `setTitle` with our title every time the date changes. – sdabet Dec 24 '13 at 15:04
  • To say it differently, every time the date changes, the system automatically calls `setTitle()` and we cannot avoid that. And right after we force another call to `setTitle` with the title we really want. – sdabet Dec 24 '13 at 15:05
  • I think you are not understanding me. You can override setTitle() in your extended DatePickerDialog and set whatever title you want by calling the super. This is a much cleaner solution without the need to know when and where setTitle() should be called. – AsafK Dec 24 '13 at 21:20
  • sure, but the drawback is that it's not very flexible because you need to hard-code the title in your custom class – sdabet Dec 25 '13 at 19:01
  • i went for overloaded constructor taking in String title as param and setting it to field. Avoided the setPermanantTitle() method. – binaryKarmic Aug 18 '16 at 13:36
4

Extend DatePickerDialog and override setTitle() method.

public class MyDatePickerDialog extends DatePickerDialog {

   public MyDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
       super(context, callBack, year, monthOfYear, dayOfMonth);
   }

   public void setTitle(CharSequence title) {
       super.setTitle(*<whatever title you want>*);
   }
}
AsafK
  • 2,425
  • 3
  • 32
  • 38
  • 1
    I agree with this solution. It is independent of the DatePickerDialog implementation. You could pass the title into constructor and then into a class variable if you want it to be flexible. – ashishduh Jul 30 '14 at 20:04
2

You could always use a AlertDialog and call setView passing an instance of DatePicker.

See example code below:

DatePicker datePicker = new DatePicker(getActivity());
    datePicker.setCalendarViewShown(false);

new AlertDialog.Builder(getActivity())
    .setTitle("Your title")
    .setView(datePicker)
    .create().show();

This will ensure title is only set by you.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Kevin Crain
  • 1,905
  • 1
  • 19
  • 28
2

Create a customized title as follows:

TextView title = new TextView(getActivity());
title.setText(getResources().getString(R.string.calendar_start_title));
datePickerDialog.setCustomTitle(title);
Axel
  • 3,331
  • 11
  • 35
  • 58
fmnavarretem
  • 151
  • 1
  • 4
0

Considering user experience on smaller screens, I would suggest not to keep the title permanent. Here is modification to John's code:

public class MyDatePickerDialog extends DatePickerDialog {

    private CharSequence title;

    public MyDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth, CharSequence title) {
        super(context, callBack, year, monthOfYear, dayOfMonth);
        this.title = title;
        setTitle(title);
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        super.onDateChanged(view, year, month, day);
        if (getDatePicker().getCalendarViewShown()) {
            setTitle(title);
        }
    }
}

And call the DatePickerDialog as follows:

MyDatePickerDialog datePickerDialog = new MyDatePickerDialog(this, null, 2012, 10, 10, "My Title");
Prashant Borde
  • 1,058
  • 10
  • 21