Cing Cong
Cing Cong

Reputation: 13

calendarView.setOnDateChangeListener android studio

everytime user select date the TextView only showing current date

public void initOverall(){
    TextView dateTest = (TextView)findViewById(R.id.cnvrt);
    CalendarView calendarView = (CalendarView)findViewById(R.id.calendarView);
    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String selectedDates = sdf.format(new Date(calendarView.getDate()));
            dateTest.setText(selectedDates);

        }
    });
}

Upvotes: 1

Views: 2744

Answers (3)

Anonymous
Anonymous

Reputation: 86306

java.time

public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
    LocalDate date = LocalDate.of(year, Month.values()[month], dayOfMonth);
    String selectedDates = date.toString();
    dateTest.setText(selectedDates);
}

As has been said in comments, you need to pick up the date that the user has chosen from the arguments passed to onSelectedDayChange. Unfortunately the month passed is 0-based, a number in the interval from 0 for January through 11 for December. Month.values()[month] is my trick for obtaining the correct Month object. If you prefer, you may instead just add 1:

    LocalDate date = LocalDate.of(year, month + 1, dayOfMonth);

I am further exploiting the fact that LocalDate.toString produces the format you want, so we need no explicit formatter. The format is ISO 8601.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Upvotes: 0

Seamas
Seamas

Reputation: 46

Because you just selected a date but not got it as onSelectedDayChange stage.

And the date you chosen was saved in parameters of onSelectedDayChange.

You can try the following code :

public void initOverall(){
        final TextView dateTest = (TextView)findViewById(R.id.cnvrt);
        final CalendarView calendarView = (CalendarView)findViewById(R.id.calendarView);
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String selectedDates = sdf.format(new Date(year-1900,month,dayOfMonth));
                dateTest.setText(selectedDates);
            }
        });
    }

Upvotes: 3

Erselan Khan
Erselan Khan

Reputation: 845

CalendarView v = new CalendarView( this );
 v.setOnDateChangeListener( new CalendarView.OnDateChangeListener() {
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
       this.calendar = new GregorianCalendar( year, month, dayOfMonth );
    }//met
 });

Upvotes: -1

Related Questions