dazza5000 dazza5000 - 5 months ago 28
Android Question

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();
}

Answer

I wasn't able to get this to work using the suggestion from nitzanj, but I switched how I created the DatePickerDialog and it works as expected now. The complete source is below. The key difference is in onCreateDialog.

public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener{

    public static final String EXTRA_DATE =
            "com.amicly.acaringtext.date";

    private static final String ARG_DATE = "date";

    @NonNull
    @Override
    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);

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public static DatePickerFragment newInstance(Date date) {
        Bundle args = new Bundle();
        args.putSerializable(ARG_DATE, date);
        DatePickerFragment fragment = new DatePickerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private void sendResult(int resultCode, Date date) {
        if (getTargetFragment() == null) {
            return;
        }
        Intent intent = new Intent();
        intent.putExtra(EXTRA_DATE, date);

        getTargetFragment().
                onActivityResult(getTargetRequestCode(), resultCode, intent);
    }

    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
        Date date = new GregorianCalendar(year, month, day).getTime();
        sendResult(Activity.RESULT_OK, date);
    }
}
Comments