Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Impossible to make my DatePickerDialog use a spinner style programmatically

I'm using a DialogFragment to open a DatePickerDialog

public class DatePickerFragment extends DialogFragment{

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);


    // Create a new instance of DatePickerDialog and return it
    DatePickerDialog DatePickerDialog = new DatePickerDialog(getActivity(), (ProfileCreationActivity)getActivity(), year, month, day);
    return DatePickerDialog;
}

I'm getting a calendar look, where I would prefer a spinner look.

I tried:

datePickerDialog.getDatePicker().setCalendarViewShown(false);

and

datePickerDialog.getDatePicker().setLayoutMode(1);

but it does not work.

Please note that I want the spinner look for one activity, but that I will want the calendar view for another activity. So I can not change the whole application style. I need a custom style for one activity.

like image 798
Lumberjack Avatar asked May 28 '15 14:05

Lumberjack


2 Answers

I have found a the explanation in the following post (which describes a problem very similar to mine) :

Android Material Design Inline Datepicker issue

In fact the setCalendarViewShown(false) and setSpinnersShown(true) are apparently not working anymore in latest versions.

We have to use an explicit XML attribute like this one android:datePickerMode="spinner".

The problem is that I'm using a DialogFragment without any XML layout (just a date picker dialog). So I cannot set any XML attribute.

The solution is to create a dedicated custom dialog with an XML layout file using the requested attribute.

like image 138
Lumberjack Avatar answered Nov 08 '22 22:11

Lumberjack


You can keep it programatically, don't need to create a new XML with the spinner, I simply changed my AppTheme(v21) style and worked ;-)

<style name="AppTheme"  parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorControlActivated">@color/colorPrimary</item>
    <item name="android:timePickerDialogTheme">@style/PickerDialogCustom</item>
    <item name="android:datePickerDialogTheme">@style/PickerDialogCustom</item>
    <item name="alertDialogTheme">@style/AlertDialogCustom</item>
</style>

<style name="PickerDialogCustom" parent="AlertDialogCustom">
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:textColorPrimary">@color/colorPrimaryDark</item>
    <item name="colorControlNormal">@color/greyLight500</item>
    <item name="android:layout_margin">2dp</item>
    <item name="android:datePickerMode">spinner</item>
</style>

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:positiveButtonText">@color/colorPrimary</item>
    <item name="android:negativeButtonText">@color/greyDark200</item>
    <item name="buttonBarNegativeButtonStyle">@style/negativeButton</item>
    <item name="android:datePickerStyle">@style/PickerDialogCustom</item>
</style>

remember to keep the support for <21 just adding this line, this command is ignored for >=21

datePickerDialog.getDatePicker().setLayoutMode(1);
like image 20
Fernando Bonet Avatar answered Nov 08 '22 22:11

Fernando Bonet