How to capture the date value with DatePicker

0

I have a DatePicker in my project and would like to know how to get the value of the chosen date after you select it.

I know that in calenderView it looks like this:

 calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {

I would like to know how to do something like DatePicker

Prints of errors:

    
asked by anonymous 24.02.2018 / 03:40

2 answers

1

No need to add new . That way you would be creating a class with the interface DatePicker.OnDateChangedListener .

The correct way is to capture through findViewById or similar and add this event to setOnDateChangedListener method.

Example:

DatePicker datePicker = findViewById(R.id.datePicker);

datePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        System.out.println( String.format(Locale.CANADA, "%d/%d/%d", dayOfMonth, monthOfYear, year) );
    }
});

In versions under 25, use the init method:

Example:

DatePicker datePicker = findViewById(R.id.datePicker);

Calendar calendar = Calendar.getInstance();

datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        System.out.println( String.format(Locale.CANADA, "%d/%d/%d", dayOfMonth, monthOfYear, year) );
    }
});
    
24.02.2018 / 04:23
1

One way to do this is by using the Android DatePicker:

Create a class that extends DialogFragment and implements the interface DatePickerDialog.OnDateSetListener .

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    private Listener listener;

    public static DatePickerFragment getInstance(){
       return new DatePickerFragment();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        //TODO criar metodo para formatar data
        String date = year + " " + month + " " + day;

        if(listener != null){
            listener.onDatePicked(data);
        }
    }

    public interface Listener{
        void onDatePicked(String data);
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }
}

And then use it anywhere:

DatePickerFragment datePickerFragment = DatePickerFragment.getInstance();
datePickerFragment.setListener(new DatePickerFragment.Listener() {
    @Override
    public void onDatePicked(String date) {
        //TODO fazer algo com a data retornada
    }
});
    
24.02.2018 / 04:26