Sasuke Uchiha
Sasuke Uchiha

Reputation: 900

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

Upvotes: 0

Views: 11432

Answers (4)

Sebastian Muñoz
Sebastian Muñoz

Reputation: 59

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

Upvotes: 1

Satyam Anand
Satyam Anand

Reputation: 377

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.

Upvotes: 0

Sasuke Uchiha
Sasuke Uchiha

Reputation: 900

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.");
        }
    }
}

Upvotes: 9

user6384551
user6384551

Reputation:

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

Upvotes: 1

Related Questions