Unable to make my DatePickerDialog programmatically spinner

I use DialogFragment to open 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 get a look on the calendar where I would prefer the spinner to look.

I tried:

 datePickerDialog.getDatePicker().setCalendarViewShown(false); 

and

 datePickerDialog.getDatePicker().setLayoutMode(1); 

but that will not work.

Please note that I want the spinner to view one action, but I want the calendar view for another action. Therefore, I can’t change the whole style of the application. I need an individual style for one action.

+6
source share
2 answers

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

Problem with Android Inline Datepicker Content

In fact, setCalendarViewShown (false) and setSpinnersShown (true) seem to no longer work in recent versions.

We should use an explicit XML attribute similar to this android: datePickerMode = "spinner" .

The problem is that I am using DialogFragment without an XML layout (date picker dialog only). Therefore, I cannot set any XML attribute.

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

+7
source

You can save it programmatically, no need to create new XML using spinner, I just changed AppTheme (v21) style and it 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> 

don't forget to keep support <21 just by adding this line, this command is ignored when> = 21

 datePickerDialog.getDatePicker().setLayoutMode(1); 
+3
source

Source: https://habr.com/ru/post/982789/


All Articles