Sasuke Uchiha Sasuke Uchiha - 6 months ago 310
Android Question

Android Datepicker - Always show date month year spinner layout instead of calendar view

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

Answer

I used this answer http://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.");
        }
    }
}
Comments