7

enter image description here

In Android 4.0 default calendar if we want to move to next month then we need to scroll the calendar but I need to move to the next or previous month by pressing arrows existed in the above image. And also I want to add to the default calendar. If add the event then that date should be marked like in the image.

I used this CalendarView.

<CalendarView
    android:id="@+id/calendarView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

I want to know how to use add event to this CalendarView pragmatically.

Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
Venkat
  • 3,447
  • 6
  • 42
  • 61
  • http://stackoverflow.com/a/7892512/1037074 This is what you will need – adam2510 Aug 28 '12 at 06:17
  • @adam2510 In 4.0 directly we drag and drop the calendar view and then how to add the event on that. But in the given code there is no code regarding this. – Venkat Aug 28 '12 at 06:27
  • This might help https://developer.android.com/guide/topics/providers/calendar-provider.html take a lok at the Update Events section – adam2510 Aug 28 '12 at 06:36
  • Plese check below link . http://stackoverflow.com/questions/7859005/how-to-read-and-edit-android-calendar-events-using-the-new-android-4-0-ice-cream/7892512#7892512 This may help you. – Nirav Ranpara Oct 06 '12 at 19:30

1 Answers1

7

To move to next or previous month you can use this code:

    mButtonNext = (Button) findViewById(R.id.buttonNext);
    mButtonNext.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar cal = new GregorianCalendar();
            cal.setTimeInMillis(mCalendarView.getDate() );
            cal.add(Calendar.MONTH, 1);
            mCalendarView.setDate(cal.getTimeInMillis(), true, true);
        }
    });

    mButtonPrevious = (Button) findViewById(R.id.buttonPrevious);
    mButtonPrevious.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar cal = new GregorianCalendar();
            cal.setTimeInMillis(mCalendarView.getDate() );
            cal.add(Calendar.MONTH, -1);                
            mCalendarView.setDate(cal.getTimeInMillis(), true, true);
        }
    });

If you want to add an event in the default calendar, you have to see the link @adam2510 posted. Here is a sample code you can use:

public void addEvent() {   
    ContentResolver cr = mContext.getContentResolver();
    ContentValues eventValues = new ContentValues();
    eventValues.put(Events.TITLE, "title");
    eventValues.put(Events.EVENT_LOCATION, "location");
    eventValues.put(Events.DTSTART, startTimeMilliseconds);
    eventValues.put(Events.DTEND, endTimeMilliseconds);
    eventValues.put(Events.CALENDAR_ID, "1");//Defaul calendar
    eventValues.put(Events.EVENT_TIMEZONE, TimeZone.SHORT);
    cr.insert(Events.CONTENT_URI, eventValues);
}

But CalendarView is not made to be used with events. In Android documentation you can read this for CalendarView:

"This class is a calendar widget for displaying and selecting dates".

I couldn't find an easy way to change cell property.
If you want to add event in your own caledar is better to implement your own calendar where you can change all views' properties. There is plenty of calendar implementations you can find in google.com

Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62