I have a custom DialogFragment
, which contains two DatePicker
widgets (from and to, using layout/custom_date_picker).
I need change the layout of dialogFragment when the screen is rotated to landscape (using layout-land/custom_date_picker).
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);
}
}
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:
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.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?.