0

I am trying to use a DatePicker:
My code is:

DatePicker datePicker = (DatePicker) v.findViewById(R.id.dialog_date_datePicker);
        datePicker.init(year, month, day, new OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int month,
                    int day) {
                mDate = new GregorianCalendar(year, month, day).getTime();
                getArguments().putSerializable(EXTRA_DATE, mDate);
            }
        });

But when I select a date the onDateChanged method is never called. After googling I found that if I changed my date picker layout to include: android:datePickerMode="spinner" /> and remove android:calendarViewShown="false" the onDateChanged is called and the code works but the date picker dialog is really bad looking.
My layout is:

<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_date_datePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:datePickerMode="spinner" />
    <!--  android:calendarViewShown="false" -->  

So why is the calenderViewShown attribute causing a problem and how can I improve the look of the date picker UI dialog?

Jim
  • 18,826
  • 34
  • 135
  • 254

2 Answers2

0

If you want to use datepicker dialog than there's no need to use DatePicker just do it this way:

    Calendar c = Calendar.getInstance();
    DatePickerDialog datePickerDialog = new DatePickerDialog(SignUpActivity.this, android.R.style.Theme_Holo_Light_Dialog_MinWidth, new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            // TODO Auto-generated method stub              
            try {
                Date d = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).parse(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);                            


            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));      

    datePickerDialog.getDatePicker().setCalendarViewShown(false);   
    datePickerDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);        
    datePickerDialog.show();
Prashant Patel
  • 1,087
  • 11
  • 18
  • So does that mean that DatePicker is "useless"? – Jim Mar 07 '15 at 10:47
  • In your example how do I start this from the fragment. I see you bind it with an activity instead. – Jim Mar 07 '15 at 10:49
  • If you need a dialog than its feasible to use DatePickerDialog and you can use DatePicker view in case you want to make your own dialog or any customization according to your need. – Prashant Patel Mar 07 '15 at 10:55
  • For fragment you can use getActivity() instead. – Prashant Patel Mar 07 '15 at 10:56
  • But where should I add this snippet? Right now I have my code in `public Dialog onCreateDialog(Bundle savedInstanceState)` of the fragment and I do: `View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_date, null);.....return new AlertDialog.Builder(getActivity()).....` – Jim Mar 07 '15 at 11:00
  • You can make a function and place the code in it and can call this function on any button click or anywhere according to your purpose. – Prashant Patel Mar 07 '15 at 11:24
0

You can also use this library : Beautiful DatePicker UI

If on AS, add this in dependencies : compile 'com.github.flavienlaurent.datetimepicker:library:0.0.2'

Also,as pointed at this question : Datepicker

You can use this to solve the issue.

<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_date_datePicker"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:calendarViewShown="false"
    android:datePickerMode="spinner" >
</DatePicker>
Community
  • 1
  • 1
  • So does that mean that DatePicker is "useless"? – Jim Mar 07 '15 at 10:47
  • It means you are beautifying the UI because you don't like the original look provided by google. –  Mar 07 '15 at 10:48
  • The DatePicker using the attribute of spinner is bad. If in my layout I remove `android:datePickerMode="spinner"` and add `android:calendarViewShown="false" instead the UI is ok – Jim Mar 07 '15 at 10:51
  • Exactly. So it's your choice, you can design your application the way you want. SO can only give you suggestions. –  Mar 07 '15 at 10:58
  • I don't follow you. So the `android:calendarViewShown=false` is broken? That is my question – Jim Mar 07 '15 at 11:01