Can't Change Android CalendarView to another month

  • Replies:6
peter nelson
  • Forum posts: 3

Jun 10, 2016, 8:34:39 PM via Website

Android introduced a CalendarView back in in API 11. I've implemented it in my app and it seems to work fine in the sense that it displays a perfectly normal-looking whole-month calendar and I can select a date and it triggers the appropriate event and I can read the selected date in my code with no problem.

imagescreen shot of my CalendarView

But I can't advance it out of the current month! The documentation says

*A user can select a date by taping on it and can scroll and fling the calendar to a desired date.*

(the "taping" appears in the documentation; I assume it's a typo for "tapping" )

I've tried flinging, swiping, scrolling and nothing happens. Is there something I need to do to enable this feature?

My XML looks like this:

<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="240dp" />

How can I scroll or fling to other dates, as the documentation says? Thanks in advance.

Reply
Vladimir S.
  • Forum posts: 266

Jun 10, 2016, 9:12:51 PM via Website

Try to add these strings to XML:

android:minDate="*your min date in mm/dd/yyyy format*"
android:maxDate="*your max date in mm/dd/yyyy format*"

Reply
peter nelson
  • Forum posts: 3

Jun 10, 2016, 10:10:26 PM via Website

That was a good idea but it didn't work. I used these but it made no difference . . .

android:minDate="01/01/2016"
android:maxDate="11/30/2016"

Reply
Vladimir S.
  • Forum posts: 266

Jun 11, 2016, 7:51:24 AM via Website

Try to set other background color, maybe arrows which are used to change month have black color abd you can't see them.

Reply
peter nelson
  • Forum posts: 3

Jun 13, 2016, 5:34:49 PM via Website

We're talking about the native, Android CalendarView class - what makes you think it has arrows? As the documentation says, you change dates by swiping or flinging.

Reply
Vladimir S.
  • Forum posts: 266

Jun 13, 2016, 10:31:17 PM via Website

Ok :)

This works on my Android 4.4 and I can scroll the months up/down:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

        @Override
        public void onSelectedDayChange(CalendarView view, int year,
                                        int month, int dayOfMonth) {
            int mYear = year;
            int mMonth = month;
            int mDay = dayOfMonth;
            String selectedDate = new StringBuilder().append(mMonth + 1)
                    .append("-").append(mDay).append("-").append(mYear)
                    .append(" ").toString();
            Toast.makeText(getApplicationContext(), selectedDate, Toast.LENGTH_LONG).show();

        }
    });
}
}

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<CalendarView
    android:id="@+id/calendarView"
    android:layout_width="match_parent"
    android:layout_height="240dp" />

</LinearLayout>

Maybe it will help...

— modified on Jun 13, 2016, 10:35:33 PM

Reply
Ashish Tripathi
  • Forum posts: 211

Jun 16, 2016, 2:50:17 PM via Website

check your swip is working or not. may be there are some exceptions. Share

Reply