DatePicker in DialogFragment ignores setCalendarViewShown

I wanted to show the DatePicker in a dialog box:

public class DatePickerDialogFragment extends DialogFragment { private OnDateSetListener dateSetListener = null; private String title = null; public DatePickerDialogFragment() {} public DatePickerDialogFragment(OnDateSetListener dateSetListener, String title) { this.dateSetListener = dateSetListener; this.title = title; } public Dialog onCreateDialog(Bundle savedInstanceState) { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); DatePickerDialog datePickerDialog = new DatePickerDialog(this.getActivity(), this.dateSetListener, year, month, day); datePickerDialog.getDatePicker().setCalendarViewShown(false); datePickerDialog.setTitle(this.title); return datePickerDialog; } } 

Unfortunately, datePickerDialog.getDatePicker().setCalendarViewShown(false); completely ignored. I hate the calendar because it is ugly and uncomfortable. So how can I turn it off?

+6
source share
2 answers

In my "values-v21 / styles.xml", I changed the activity theme as shown below, hiding the calendar and showing a simple date picker counter. You can use painting with a white background and rounded corners:

  <item name="android:datePickerDialogTheme">@style/style_date_picker_dialog</item> </style> <style name="style_date_picker_dialog" parent="@android:style/Theme.DeviceDefault.Light"> <item name="android:windowIsFloating">true</item> <item name="android:windowBackground">@drawable/dialog_background</item> <item name="android:datePickerStyle">@style/style_datepicker</item> </style> <style name="style_datepicker" parent="android:Widget.Material.Light.DatePicker"> <item name="android:datePickerMode">spinner</item> </style> 
+10
source

This was answered in another place: The problem with the materials for Android Inline Datepicker , and trying to solve it with the accepted answer did not get me.

Basically you need to do:

 <DatePicker ... android:datePickerMode="spinner" /> 

because setCalandarShown(); outdated.

0
source

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


All Articles