you can use google calendar in order to display your new calendar and over here you can create your own events. Below is the class for creating a new calendar.
public class CalendarMapper {
private static final String ACCOUNT_NAME = "private";
private static final String INT_NAME_PREFIX = "priv";
@SuppressLint("NewApi")
private static Uri buildCalUri() {
return CalendarContract.Calendars.CONTENT_URI
.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(Calendars.ACCOUNT_NAME, ACCOUNT_NAME)
.appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
.build();
}
private static ContentValues buildContentValues(Calendar calendar) {
String dispName = "TUM3"; //Calendar.getName() returns a String
String intName = INT_NAME_PREFIX + dispName;
final ContentValues cv = new ContentValues();
cv.put(Calendars.ACCOUNT_NAME, ACCOUNT_NAME);
cv.put(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
cv.put(Calendars.NAME, intName);
cv.put(Calendars.CALENDAR_DISPLAY_NAME, dispName);
//cv.put(Calendars.CALENDAR_COLOR, calendar.getColor()); //Calendar.getColor() returns int
cv.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
cv.put(Calendars.OWNER_ACCOUNT, ACCOUNT_NAME);
cv.put(Calendars.VISIBLE, 1);
cv.put(Calendars.SYNC_EVENTS, 1);
return cv;
}
@SuppressWarnings("deprecation")
public static Uri addCalendar(Calendar calendar, ContentResolver cr) {
if (calendar == null)
throw new IllegalArgumentException();
final ContentValues cv = buildContentValues(calendar);
Uri calUri = buildCalUri();
Uri cancelUri=cr.insert(calUri, cv);
return cancelUri;
}
}
In the main activity you can call add calendar to create a calendar and add an event into this calendar.
Uri cancelUri;
@SuppressLint("NewApi")
public void displayCal(View view){
ContentResolver crv = getContentResolver();
Calendar calendar = Calendar.getInstance();
cancelUri= CalendarMapper.addCalendar(calendar, crv);
long calID=2; //ID of the newly created calendar. You can query the calendars table to get the ID of your calendar
long endMillis = 10100;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
long startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = getContentResolver();
ContentValues values2 = new ContentValues();
values2.put(Events.DTSTART, startMillis);
values2.put(Events.DTEND, endMillis);
values2.put(Events.TITLE, "Jazzercise");
values2.put(Events.DESCRIPTION, "Group workout");
values2.put(Events.CALENDAR_ID, calID);
values2.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
Uri uri = cr.insert(Events.CONTENT_URI, values2);
Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
builder.appendPath("time");
ContentUris.appendId(builder, startMillis);
Intent intent = new Intent(Intent.ACTION_VIEW)
.setData(builder.build());
startActivity(intent);
}
In order to delete this calendar, you can do it like below
public void delCal(View view){
ContentResolver crv = getContentResolver();
crv.delete(cancelUri, null, null);
}