0

I am getting an extra header for DatePickerDialog as shown in the screenshot below. However, my question is not about how to remove this extra header but it's about the cause behind this. First of all, let me copy and paste my DatePickerDialog code even though it's fairly simple and straightforward.

//initialize dialog
datePickerDialog = new DatePickerDialog(
        getActivity(),
        R.style.DatePickerTheme,
        this,
        today.get(Calendar.YEAR),
        today.get(Calendar.MONTH),
        today.get(Calendar.DAY_OF_MONTH));

//set dialog maximum date
datePickerDialog.getDatePicker().setMaxDate(maxDate.getTimeInMillis());

//set dialog minimum possible date
datePickerDialog.getDatePicker().setMinDate(minDate.getTimeInMillis());

datePickerDialog.show();

As you can see from the attached screenshot, there is an extra title(or header) in the date picker. I was trying to google for solutions and potentially understand the cause behind. I found there are already 2 similar questions(q1, q2) asked on stackoverflow about extra title and the solution is also pretty clear and straightward. We can do either:

  1. datePickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); or
  2. datePickerDiaglog.setTitle("");

to eliminate the extra title. However, no one really explained the cause behind this. Also, the most important thing is that the extra title problem only appears on Android Marshmallow, Lollipop and potentially previous versions as well but not on Android Nougat when I tested it in the emulator. I tried to trace down through the source code and inspired from one of the answer in this question asked here, someone said:

The following line of code sets your dialog title value: fromDatePickerDialog.getDatePicker().setMaxDate(d.getTime());

So I tried to look into the source code of DatePickerDialog on different version of APIs(23 and 24, especially on 23 where it has the extra title problem) but did not find that setMaxDate is setting the extra title. I am getting to a dead end and stuck on this for a while. Can someone please point to a direction on this or share your experience if you have seen this before? Many Thanks!

enter image description here

SSDong
  • 145
  • 2
  • 10

1 Answers1

0

Wow 3 months and no answer I am not sure you can remove that additional date in the header I would look at all the styling properties that DatePicker has first Second here is some XML styles code for a DatePicker that lives in its own XML file and the corresponding code that creates a DatePicker Dialog and uses the XML file Dialog is called with onClick of a button

Dialog Code First

    public void findByDate(View view){
    use= false;
    // custom dialog /* THIS R.style.DatePickerThem is mandatory to use res/values/styles */
    final Dialog dialog = new Dialog(MainActivity.this,R.style.DatePickerTheme);
    dialog.setContentView(R.layout.datepickerview);
    dialog.setTitle("");
    DatePicker picker = dialog.findViewById(R.id.datePicker);
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
    picker.updateDate(mYear, mMonth, mDay);// Keeps Calendar initial view what ever today is!

    picker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
        //picker.init(Integer.valueOf(mMonth),Integer.valueOf(mDay),Integer.valueOf(mYear), new DatePicker.OnDateChangedListener() {
        @Override
        public void onDateChanged(DatePicker picker, int year, int monthOfYear, int dayOfMonth) {

            if (etFromDate.getText().toString().isEmpty()) {

                System.out.println("I am Not Empty");

                String searchFrom = (String.valueOf(monthOfYear+1))+(String.valueOf(dayOfMonth))+String.valueOf(year);
                if(searchFrom.length()==7){
                    StringBuilder str = new StringBuilder(searchFrom);
                    str.insert(2, '0');
                    etFromDate.setText(str);
                }else if (searchFrom.length() == 6){
                    StringBuilder str = new StringBuilder(searchFrom);
                    str.insert(0,'0');
                    str.insert(2,'0');
                    etFromDate.setText(str);
                }else {
                    etFromDate.setText(searchFrom);
                }

                dialog.dismiss();

            } else {

                String searchTo = (String.valueOf(monthOfYear+1))+(String.valueOf(dayOfMonth))+String.valueOf(year);

                if(searchTo.length()==7){
                    StringBuilder str = new StringBuilder(searchTo);
                    str.insert(2, '0');
                    etToDate.setText(str);
                }else if (searchTo.length() ==6){
                    StringBuilder str = new StringBuilder(searchTo);
                    str.insert(0,'0');
                    str.insert(2,'0');
                    etToDate.setText(str);
                }else {
                    etToDate.setText(searchTo);
                }

                dialog.dismiss();

            }
            //mDay=dayOfMonth;
            //mMonth=(month);
            //mYear=year;
        }
    });

    dialog.show();
}

Date Picker XML File

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_White"
android:orientation="vertical">

<DatePicker
    android:id="@+id/datePicker"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:calendarViewShown="true"
    android:headerBackground="@color/color_Purple"
    android:theme="@style/DatePickerTheme" />

Here is the Styles for the Date Picker

    <style name="DatePickerTheme"  parent="Theme.AppCompat.Light.Dialog">
    <item name="colorControlActivated">@color/colorAccent</item>
    <item name="android:selectableItemBackgroundBorderless">@color/color_Purple</item>
    <item name="android:headerBackground">@color/color_Purple</item>
    <item name="android:textColorPrimary">@color/color_Black</item>
</style>
Vector
  • 3,066
  • 5
  • 27
  • 54