0

I have a custom DialogFragment, which contains two DatePicker widgets (from and to, using layout/custom_date_picker).

enter image description here

I need change the layout of dialogFragment when the screen is rotated to landscape (using layout-land/custom_date_picker).

enter image description here

The configuration of the Activity is :

<activity
        android:name=".activities.MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:launchMode="singleTop"/>

The implemented DialogFragment source is:

public class DatePickerDialog extends DialogFragment
{

public static final String START_SEARCH_DATE = "startSearchDate";
public static final String END_SEARCH_DATE = "endSearchDate";
private CustomDatePicker dpStartDate;
private CustomDatePicker dpEndDate;
private AlertDialog d;

public interface DatePickerDialogListener
{
    void onFinishDatePickDialog(String fromDate, String toDate);
}

public DatePickerDialog()
{
}

// Use this instance of the interface to deliver action events
DatePickerDialogListener mListener;

// Override the Fragment.onAttach() method to instantiate the DatePickerDialogListener
@Override
public void onAttach(Activity activity)
{
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try
    {
        // Instantiate the NoticeDialogListener so we can send events to the host
        mListener = (DatePickerDialogListener) activity;
    } catch (ClassCastException e)
    {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString() + " must implement DatePickerDialogListener");
    }
}

public static DatePickerDialog newInstance(String startSearchDate, String endSearchDate)
{
    DatePickerDialog frag = new DatePickerDialog();
    Bundle args = new Bundle();
    args.putString(START_SEARCH_DATE, startSearchDate);
    args.putString(END_SEARCH_DATE, endSearchDate);
    frag.setArguments(args);
    return frag;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    View customView = getActivity().getLayoutInflater().inflate(R.layout.custom_date_picker, null);
    // Define your date pickers
    dpStartDate = (CustomDatePicker) customView.findViewById(R.id.dpStartDate);
    dpEndDate = (CustomDatePicker) customView.findViewById(R.id.dpEndDate);

    String startSearchDate = getArguments().getString(START_SEARCH_DATE);
    String endSearchDate = getArguments().getString(END_SEARCH_DATE);

    dpStartDate.updateDate(startSearchDate);
    dpEndDate.updateDate(endSearchDate);

    d = new AlertDialog.Builder(getActivity())
            .setView(customView)
            .setTitle(getString(R.string.datepicker_hint))
            .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {

                }
            }) //Set to null. We override the onclick
            .setNegativeButton(getString(R.string.cancel), null)
            .create();
    d.show();
    d.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

            boolean b = DateUtils.largerThanEndDate(dpStartDate, dpEndDate);
            if (b)
            {
                ToastUtils.showShortText(getActivity(), getString(R.string.date_error));
            } else
            {
                mListener.onFinishDatePickDialog(dpStartDate.getDate(), dpEndDate.getDate());
                dismiss();
            }
        }
    });
    return d;
}
}

Can anyone please guide me as to how this can be done ? Thanks in advance.

Edit: Here the code with which I invoke the DialogFragment:

public void showDatePicker()
{
    startSearchDate = dateModel.getStartDate();
    endSearchDate = dateModel.getEndDate();
    datePickerDialog = (DatePickerDialog)fragmentManager.findFragmentByTag(DATE_PICKER_DIALOG);

    if (alertDialog == null)
    {
        alertDialog = DatePickerDialog.newInstance(startSearchDate, endSearchDate);
        alertDialog.show(fragmentManager, DATE_PICKER_DIALOG);
    }

}
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
Tim Yu
  • 1,243
  • 2
  • 9
  • 10
  • @Y.S. Now I click the button and show the dialog,but the layout can't be changed when the screen is rotated. – Tim Yu Jun 09 '15 at 01:28

1 Answers1

1

I suppose you could consider using an if clause in your onCreate() as follows:

int rotation = getWindowManager().getDefaultDisplay().getRotation();

and then do

switch(rotation){
    case Surface.ROTATION_0: 
    case Surface.ROTATION_180:
        // show portrait

    case Surface.ROTATION_90: 
    case Surface.ROTATION_270:
        // show landscape
}

For portrait mode, show the two dialogs stacked. And for landscape mode, show them side-by-side.

EDIT:

Answering point-wise:

  • When the phone is rotated, the Activity is necessarily re-created. This means that onCreate() will be called on rotation. Hence, the rotation state can be determined in onCreate() as shown above. In this case, make rotation a class member field so that it can be accessed throughout the class.
  • Indeed, as you correctly point out, the dialog can be shown from either onConfigurationChanged() or in onCreate() itself. For this, and for how to save the instance state, please see the attached links.

References:

1. How to properly retain a DialogFragment through rotation?.

2. Why won't Fragment retain state when screen is rotated?.

Community
  • 1
  • 1
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • I think I should show the dialog in onConfigurationChanged method,but I am not sure how to change the layout of dialog and save the state of datepicker. – Tim Yu Jun 08 '15 at 08:34
  • Thanks your info. I have read the references,but can't solve my problem.Please see my edited.How can I change the dialog layout (just invoking the showDatePicker() can't work properly)from onConfigurationChanged method? – Tim Yu Jun 08 '15 at 13:37
  • did you manage to solve this? I have the same problem – Gustavo Ross Nov 03 '22 at 15:10