How to capture date value with Datepicker

Asked

Viewed 974 times

0

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

I know that on the calendar:

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

I’d like to know how to do something like Datepicker

Prints of errors:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

2 answers

1


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

The correct thing is for you to capture through findViewById or similar and add that event in the method setOnDateChangedListener.

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 less than 25, use the method init:

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) );
    }
});
  • In the first example it shows "cannot resolve method 'setOnDateChangedListener...'"

  • I’ll test the second agr

  • Ah, and on the new, was suggested the android itself stuido due to the error, the prints follow the errors in sequence

1

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

Create a class that extends DialogFragment and implement 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 wherever you want:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.