Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add events in Android 4.0 default calendar?

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.

like image 838
Venkat Avatar asked Aug 28 '12 04:08

Venkat


1 Answers

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

like image 148
Mario Kutlev Avatar answered Oct 20 '22 01:10

Mario Kutlev