1

I'm having the same issue as this thread: Android Material Design Inline Datepicker issue, but I am not using an XML layout, instead I'm building the DatePicker programmatically.

This is the code I am using but is not working

DatePicker dpView = new DatePicker(ctx);
dpView.setCalendarViewShown(false);
dpView.setSpinnersShown(true);

how can I make it work?

Cœur
  • 37,241
  • 25
  • 195
  • 267
ludwiguer
  • 2,177
  • 2
  • 15
  • 21

4 Answers4

24

If you have to set it up programmatically in a DatePickerFragment just set this:

DatePickerDialog dialog = new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Dialog_MinWidth, this, year, month, day);

with whatever style you like.

Luismi
  • 1,071
  • 1
  • 13
  • 18
9

The problem in Android 5.0 is the "mode" which determines whether to use a calendar or not is read at construct time, and in code you can't set the mode until after it has been constructed, thus it's too late. (Source is here: DatePicker Source Code)

My solution was to create my own reusable DatePicker layout that specifies the "no calendar" mode, and construct my DateTimes programmatically with that layout instead of Android's default.

Bottom line is, create a "DatePicker.axml" file, put it in the resources folder, and paste the following as its contents:

<?xml version="1.0" encoding="utf-8"?>

<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:calendarViewShown="false"
          android:datePickerMode="spinner"/>

and declare it wherever you need in code like this:

LayoutInflater inflater = LayoutInflater.From( Activity );
DatePicker datePicker = (DatePicker)inflater.Inflate( Resource.Layout.DatePicker, null );
JHawkZZ
  • 246
  • 3
  • 8
7

Adding to the answer from user Luismi:

The theme Theme.Holo.Dialog or Theme.Holo.Dialog.MinWidth might cause problems like two boxes drawn around the date picker.

Instead you should the theme Theme.Holo.Dialog.NoFrame to prevent this. The theme might not be accessable from your code, so just create your own theme:

<style name="DatePickerDialogTheme" parent="android:Theme.Holo.Light.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@null</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowCloseOnTouchOutside">false</item>
</style>

Edit: The better way to fix this problem is to use the SpinnerDatePicker library that recreates the old design.

tharkay
  • 5,913
  • 2
  • 26
  • 33
1

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>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Fernando Bonet
  • 576
  • 5
  • 13