I want to change DatePickerDialogs and TimePickerDialogs globally using their spinner style. For that case I added android:datePickerStyle and android:timePickerStyle to my AppTheme. When I was testing the style changes, everything is fine on a virtual emulated Nexus device. But when I test it on an Samsung device, there where noch changed styles loaded. Both devices run with Nougat.
Here is a snippet from my style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:datePickerStyle">@style/AppTheme.DatePicker</item>
<item name="android:timePickerStyle">@style/AppTheme.TimePicker</item>
</style>
<style name="AppTheme.DatePicker" parent="android:Widget.DatePicker">
<item name="android:spinnersShown">true</item>
<item name="android:calendarViewShown">false</item>
</style>
<style name="AppTheme.TimePicker" parent="android:Widget.Material.TimePicker">
<item name="android:timePickerMode">spinner</item>
</style>
And that is the way I call the DatePickerDialog. (The TimePickerDialogs are called the same way)
DatePickerDialog datePickerDialog = new DatePickerDialog(
getView().getContext(), SettingsFragment.this, 2000 , 10, 1);
datePickerDialog.show();
Settings.Fragment is the fragment that calls the dialogs.
This it how it should look Nexus device
This it how it should not look Samsung device
Edit: The Nexus device is a virtual emulated Nexus 5X with Android 7.1.1 and the Samsung device is a Samsung S7 Edge wit Android 7.0.
I found a workaround for the problem myself. The workaround is not to use the global styles. Instead I had to write my own DatePickerDialog.
In the custom dialog layout I could use the DatePicker preferences I wanted. This looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<DatePicker
android:id="@+id/spinnerDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:focusable="true"
android:spinnersShown="true" />
</LinearLayout>
The custom dialog looks like the following:
public class SpinnerDatePickerDialog extends DialogFragment {
private ISpinnerDatePickerDialogListener listener;
private DatePicker datePicker;
// this is no clean android fragment constructor, but we want to use it here
@SuppressLint("ValidFragment")
public SpinnerDatePickerDialog(ISpinnerDatePickerDialogListener listener) {
super();
this.listener = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Build the dialog and set up the button click handlers
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_date, null);
datePicker = (DatePicker) view.findViewById(R.id.spinnerDatePicker);
datePicker.updateDate(defaultYear, defaultMonth, defaultDay);
builder.setView(view);
builder
.setPositiveButton(R.string.submit, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host activity
listener.onSpinnerDateDialogPositiveClick(SpinnerDatePickerDialog.this);
}
})
.setNegativeButton(R.string.abort, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity
listener.onSpinnerDateDialogNegativeClick(SpinnerDatePickerDialog.this);
}
});
return builder.create();
}
}
To send values to the original fragment, I created an interface. The original fragment implements this interface and the custom dialog gets the fragment as an parameter in his constructor. This way I can trigger the listener in the original fragment. The interface:
public interface ISpinnerDatePickerDialogListener {
void onSpinnerDateDialogPositiveClick(SpinnerDatePickerDialog dialog);
void onSpinnerDateDialogNegativeClick(SpinnerDatePickerDialog dialog);
}
The fragment class that calls the dialog:
public class SettingsFragment extends Fragment implements ISpinnerDatePickerDialogListener, ISpinnerTimePickerDialogListener {
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// initialize listeners for text inputs, to open picker dialogs
periodBegin = (EditText) getView().findViewById(R.id.editPeriodBegin);
periodBegin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(PERIOD_BEGIN_DIALOG_ID);
}
});
}
/**
* opens dialog for id
*
* @param id
*/
private void showDialog(int id) {
switch(id) {
case PERIOD_BEGIN_DIALOG_ID:
SpinnerDatePickerDialog datePickerDialog;
datePickerDialog = new SpinnerDatePickerDialog(this);
datePickerDialog.show(getFragmentManager(), datePickerDialog.getTAG());
break;
}
@Override
public void onSpinnerDateDialogPositiveClick(SpinnerDatePickerDialog dialog) {
// TODO send DatePicker values with listener
// load DatePicker from dialog and set them to EditText text
DatePicker datePicker = dialog.getDatePicker();
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year = datePicker.getYear();
// TODO
String formattedDate = FORMAT.format(new Date(year - 1900, month, day));
periodBegin.setText(formattedDate);
}
@Override
public void onSpinnerDateDialogNegativeClick(SpinnerDatePickerDialog dialog) {
// Nothing to do here
}
}