vlad-ardelean
vlad-ardelean

Reputation: 7622

CalendarView getDate method returns current date, not selected date... What am I doing wrong?

My calendarView fails to return the selected date, instead returning some default that always points to today.

I am of course changing the date selected in my calendar, and it indeed displays as having changed. I tried inspecting the view in debug mode, but didn't find anything.

I am running this in a simulator, not on a real phone... Should I modify some settings? Am I missing something important? Because it really is confusing that I'm not getting the selected date, but the current one.

<CalendarView
                            android:id="@+id/view_calendar_create_event_date"
                            android:layout_width="wrap_content"
                            android:layout_height="0dp"
                            android:layout_weight="1" />

This is called from the event listener

protected void createEvent(View view){
        TextView eventNameView = (TextView) this.findViewById(R.id.createEventNameInput);
        String eventName = eventNameView.getEditableText().toString();

        CalendarView eventOccursOnView = (CalendarView) this.findViewById(R.id.view_calendar_create_event_date);
        long eventOccursOn = eventOccursOnView.getDate();
        Date temporary = new Date(eventOccursOn);

        Event newEvent = new Event(eventName, "", 0, 0, eventOccursOn);
        newEvent.save(view.getContext());
    }

and this is how I'm setting my event listener

saveButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                // Create the event
                EventDetailsActivity.this.createEvent(view);

                // Notify the user
                Snackbar.make(view, "Successfully created a new event!", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

                // Return to the previous activity
                finish();
            }
        });

Upvotes: 11

Views: 8071

Answers (2)

Myroslav
Myroslav

Reputation: 1237

This is my understanding and solution:

by default CalendarView's selected date is today. When you select new day in calendar, it's just highlighted in view, but not actually selected (I know, doesn't make sense, but this is how this view works). So you need to implement date change listener and inside it select new date in calendar view.

import java.util.Calendar
import java.util.Date

// assuming your CalendarView has id 'calendarView'
fun initCalendarView() {
   // prints today's date by default
   printCurrentlySelectedDate()

   binding.calendarView.setOnDateChangeListener { _, year, month, dayOfMonth ->
      binding.calendarView.date = Calendar.getInstance()
         .apply { set(year, month, dayOfMonth) }
         .timeInMillis
   }
}

fun consumeSelectedDate() {
   // now date is whatever you selected in view
   printCurrentlySelectedDate()
}

fun printCurrentlySelectedDate() {
   val date = Date(binding.calendarView.date)
   println(date)
}

Upvotes: 0

Mithun Sarker
Mithun Sarker

Reputation: 4023

You need to implement setOnDateChangeListener

long eventOccursOn;

eventOccursOnView.setOnDateChangeListener(new OnDateChangeListener() {
    //show the selected date as a toast
    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
        Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);
        eventOccursOn = c.getTimeInMillis(); //this is what you want to use later
    }
 });

Upvotes: 15

Related Questions