Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DatePickerDialog save layout on screen rotate

I have strange problem. I have Activity inherite from AppCompatActivity with Fragment inside. In this Fragment I show DatePickerDialog to select date.

A declared Activity in manifest.xml as

<activity name=".SampleActivity" android:configChanges="keyboardHidden|orientation|screenLayout|screenSize" />

now. I want to show DatePickerDialog inside Fragment so I created custom DatePickertFragment with code:

public class DatePickerFragment extends AppCompatDialogFragment{
    // (...)
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
        return dialog;
    }
    // (...)
}

and execute show() method inside fragment

 pickerFragment.show(getChildFragmentManager(), null);

I run show picker code inside onClick() method. DatePicker shows correctly.

And now is problem. Dialog show correctly in portrait screen orientation but when I rotate screen to landscape mode dialog seems to be cut on left and right size. I checked and I suppose that Dialog picker didn't change layout orientation on screen rotate. Screenshots below 1. Portrait orientation, correct dialog enter image description here

  1. Next I rotate device and in landscape mode I getting something like cut dialog (landscape dialog layout in dialog portrait layout dimensions) enter image description here

I changed manifest.xml Activity declaration to

android:configChanges="keyboardHidden|orientation" 
And now is work correctly but I can't do this, because other app part will work incorrect. So I wonder if I can invalidate or redraw DialogPicker to resize it boundaries after I rotate screen?
like image 934
Zbyszko Avatar asked Mar 14 '16 12:03

Zbyszko


2 Answers

Use onConfigurationChanged(Configuration newConfig) to handle calendar crop issue checkout my code is working 100% with screenSize. startDateDpd is Date picker dialog,startDateTxt - on click of this date picker dialog opens

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if(startDateDpd!=null)
    if(startDateDpd.isVisible()){
        startDateDpd.dismiss();
        startDateTxt.performClick();
    }
}
like image 181
Sumedh Ulhe Avatar answered Sep 28 '22 10:09

Sumedh Ulhe


According to Sumedh Ulhe's answer, I find out that DatePicker/TimePicker will load layout on creating time.

You can see the android-26\android\widget\TimePicker.java

mDelegate = new TimePickerClockDelegate(

and android-26\android\widget\TimePickerClockDelegate.java

final int layoutResourceId = a.getResourceId(R.styleable.TimePicker_internalLayout,
    R.layout.time_picker_material);

There are two R.layout.time_picker_material: android-26\data\res\layout\time_picker_material.xml and android-26\data\res\layout-land\time_picker_material.xml

So I re-create the picker when picker is already opened

mCalendar = Calendar.getInstance();
...
@Override
protected void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (pickerDialog != null && pickerDialog.isShowing()) {
        pickerDialog.dismiss();
        pickerDialog = new DatePickerDialog(getContext(), (picker, year, month, 
            day) -> {
            // Do something
        }, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DATE));
        pickerDialog.show();
    }
}
like image 24
Lucas Huang Avatar answered Sep 28 '22 10:09

Lucas Huang