0

While using android datepicker, I want to always show the old date month year spinner instead of new calendar view ? How can I do that?

I want this for all device running any android version: enter image description here and dont want following ui:

enter image description here

Sasuke Uchiha
  • 890
  • 3
  • 15
  • 31
  • how you are adding date picker to your app by programetically or by xml?? – Dhiraj May 26 '16 at 08:31
  • 1
    Possible duplicate of [Hide Calenderview from DatePicker programatically in API 21](http://stackoverflow.com/questions/27709668/hide-calenderview-from-datepicker-programatically-in-api-21) – Jofre Mateu May 26 '16 at 08:31

4 Answers4

9

I used this answer https://stackoverflow.com/a/31610267/5700434 to completely solve the problem.

Mainly use this :

DatePickerDialog dialog = new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Light_Dialog_MinWidth, this, year, month, day);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Full code :

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

import java.util.Calendar;


public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    private Calendar calendar;
    private Calendar maxCalendar;
    private Calendar minCalendar;
    private DatePickerDialog.OnDateSetListener onDateSetListener;

    private static final String TAG = DatePickerFragment.class.getSimpleName();

    public void setup(Calendar calendar, DatePickerDialog.OnDateSetListener onDateSetListener){
        this.calendar = calendar;
        this.onDateSetListener = onDateSetListener;
    }

    public void setup(Calendar calendar, Calendar maxCalendar, DatePickerDialog.OnDateSetListener onDateSetListener){
        this.calendar = calendar;
        this.maxCalendar = maxCalendar;
        this.onDateSetListener = onDateSetListener;
    }

    public void setup(Calendar calendar, Calendar maxCalendar, Calendar minCalendar){
        this.calendar = calendar;
        this.maxCalendar = maxCalendar;
        this.minCalendar = minCalendar;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dialog = new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Light_Dialog_MinWidth, this, year, month, day);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        if(minCalendar!=null){
            dialog.getDatePicker().setMinDate(minCalendar.getTimeInMillis());
        }

        if(maxCalendar != null){
            dialog.getDatePicker().setMaxDate(maxCalendar.getTimeInMillis());
        }

        return dialog;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        if(onDateSetListener != null){
            onDateSetListener.onDateSet(view, year, month, day);
        }else{
            LogUtils.w(TAG, "onDateSetListener callback is not initialized.");
        }
    }
}
Community
  • 1
  • 1
Sasuke Uchiha
  • 890
  • 3
  • 15
  • 31
1

There are many libraries available for android date picker( eg: exmple1 example2 example3 . or you can use default date picker. This date picker may be the one you are looking for

0
DatePickerDialog dialog = new
DatePickerDialog(getActivity(),AlertDialog.THEME_HOLO_LIGHT, this, year, month, day);

This worked perfectly in all the version of android. from 4.4 to 6.0 tested although it is deprecated but it supported very well.

boop_the_snoot
  • 3,209
  • 4
  • 33
  • 44
Satyam Anand
  • 377
  • 1
  • 8
0

Use the DatePicker instead of the Calendar View, and add the following xml attributes:

android:datePickerMode="spinner"
android:calendarViewShown="false"

Giving the following result:

enter image description here