Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DatePicker Widget does not take up entire AlertDialog on Nexus 6

I have a DatePickerFragment that uses a very simple custom layout. The layout takes up the entire alert dialog on smaller devices (HTC One m7), but does not on the Nexus 6. Does anyone know why the DatePicker does not take up the entire width of the Nexus 6 Alert Dialog or where to begin troubleshooting? Thank you!

Nexus 6 (notice that date header does not fill the entire alert dialog)

enter image description here

HTC One m7

enter image description here

dialog_date.xml

<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/dialog_date_date_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:calendarViewShown="false">
</DatePicker>

onCreateDialog() of DatePickerFragment

public Dialog onCreateDialog(Bundle savedInstanceState) {
        Date date = (Date) getArguments().getSerializable(ARG_DATE);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        View v = LayoutInflater.from(getActivity())
                 .inflate(R.layout.dialog_date, null);

        mDatePicker = (DatePicker)
  v.findViewById(R.id.dialog_date_date_picker);
        mDatePicker.init(year, month, day, null);

        return new AlertDialog.Builder(getActivity())
                 .setView(v)
//                     .setTitle(R.string.date_picker_title)
                 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         int year = mDatePicker.getYear();
                         int month = mDatePicker.getMonth();
                         int day = mDatePicker.getDayOfMonth();
                         Date date = new GregorianCalendar(year, month, day).getTime();
                         sendResult(Activity.RESULT_OK, date);
                     }
                 })
                 .create();
        }
like image 630
dazza5000 Avatar asked Feb 05 '16 23:02

dazza5000


1 Answers

The reason is AlertDialog takes more space than the child DatePicker.

Make sure to use dialog theme (ex. Theme.AppCompat.Light.Dialog) without .Alert suffix.

like image 111
YunhaoLIU Avatar answered Sep 26 '22 01:09

YunhaoLIU