How to read and edit Android calendar events using the new Android 4.0 Ice Cream Sandwich API?
Here's code that will let you add an event directly:
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.provider.CalendarContract;
import java.util.Calendar;
// Construct event details
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
// Insert Event
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
values.put(CalendarContract.Events.TITLE, "Walk The Dog");
values.put(CalendarContract.Events.DESCRIPTION, "My dog is bored, so we're going on a really long walk!");
values.put(CalendarContract.Events.CALENDAR_ID, 3);
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
// Retrieve ID for new event
String eventID = uri.getLastPathSegment();
You'll need the CALENDAR_ID, so here's how to query the list of calendars:
Uri uri = CalendarContract.Calendars.CONTENT_URI;
String[] projection = new String[] {
CalendarContract.Calendars._ID,
CalendarContract.Calendars.ACCOUNT_NAME,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.NAME,
CalendarContract.Calendars.CALENDAR_COLOR
};
Cursor calendarCursor = managedQuery(uri, projection, null, null, null);
You'll need to request the android.permission.READ_CALENDAR
permission for all of this to work.
If you want to avoid having to request permissions, you can also ask the Calendar app to create a new event on your behalf by using an Intent
:
Intent intent = new Intent(Intent.ACTION_INSERT)
.setType("vnd.android.cursor.item/event")
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY , false) // just included for completeness
.putExtra(Events.TITLE, "My Awesome Event")
.putExtra(Events.DESCRIPTION, "Heading out with friends to do something awesome.")
.putExtra(Events.EVENT_LOCATION, "Earth")
.putExtra(Events.RRULE, "FREQ=DAILY;COUNT=10")
.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE)
.putExtra(Intent.EXTRA_EMAIL, "my.friend@example.com");
startActivity(intent);
Adding Calendar and events in Android 4.0
In ICS you have to use the public Calendar API. Please check the links below:
How to read and edit Android calendar events using the new Android 4.0 Ice Cream Sandwich API?
http://www.techrepublic.com/blog/app-builder/programming-with-the-android-40-calendar-api-the-good-the-bad-and-the-ugly/825
http://android10.org/index.php/articlestrickssecrets/353-android-ics-adding-events-to-the-calendar
http://www.vogella.com/articles/AndroidCalendar/article.html
Calendar in Android +4.0
Calendar Api has changed in 4.0+. You can use the new Calendar Intent api (this is the recommended method) or modify your event to fit with the new api:
values.add("eventTimezone","00");
remove the "visibility" key/value, it's not accepted anymore.
You should print the exception in your ExceptionCatcher, it is quite descriptive.
How to add events in Android 4.0 default calendar?
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
How to get Google Calendar events in android using google calendar api?
You need to use Calender Provider for that
and for events look at the link below :
http://developer.android.com/guide/topics/providers/calendar-provider.html#events
Here are some blogs and explaination for the same
http://www.grokkingandroid.com/androids-calendarcontract-provider/
And in this link is shown how to read and edit events in calender provider.
Hope this helps...
How to add calendar events to default calendar, silently without Intent, in android 4?
Here is a working example of what i eventually made it:
ContentResolver cr = ctx.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, dtstart);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, comment);
TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
// Default calendar
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.RRULE, "FREQ=DAILY;UNTIL="
+ dtUntill);
// Set Period for 1 Hour
values.put(CalendarContract.Events.DURATION, "+P1H");
values.put(CalendarContract.Events.HAS_ALARM, 1);
// Insert event to calendar
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
where dtuntil
is
SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
Calendar dt = Calendar.getInstance();
// Where untilDate is a date instance of your choice, for example 30/01/2012
dt.setTime(untilDate);
// If you want the event until 30/01/2012, you must add one day from our day because UNTIL in RRule sets events before the last day
dt.add(Calendar.DATE, 1);
String dtUntill = yyyyMMdd.format(dt.getTime());
Ref: Recurrence Rule
Related Topics
How to Disable Night Mode in My Application Even If Night Mode Is Enable in Android 9.0 (Pie)
After Upgrading to Google Play Services 9.0.0, App Hangs in Dynamitemodulesc
Android:Split the Screen in 2 Equals Parts with 2 Listviews
How to Combine Multiple Images into a Single Image in Android
Android: Using Activitygroup to Embed Activities
How to Share Photo with Caption via Android Share Intent on Facebook
Cache Images Local, from Google Firebase Storage
Travis Ci Failed Because Cannot Accept License Constrain Layout
Android Scrollview Layout Problem
Change Status Bar Color with Appcompat Actionbaractivity
Using JSON File in Android App Resources
Should I Use Android: Process =":Remote" in My Receiver
Position of Dialogfragment in Android
Firebase Push Notifications Update Db
Android: Getsupportactionbar() Always Returns Null in Actionbarsherlock Library
Cache.Properties (The System Cannot Find the File Specified)