After I had successfully added the calendar application to my Android emulator running Android 2.2 (Froyo), I soon realized that I wasn’t able to create events in it because I had no calendars. Android gave me this nice message: You have no calendars.
Searching the web for this problem lead me to the need of creating a Google account that then could be synced with the emulator’s calendar. However, the sync did not work. After setting up the account under Settings - Accounts and sync - Add account - Google it didn’t show up in the sync settings and there was still no calendar created for it.
The Android documentation said that it would be possible to create a calendar in an application, so I tried that. Quite a few hours of trying and debugging later, I finally found this post on StackOverflow, that contains a list of all the needed fields and correct values for them: After adding an event to a user-created calendar in android,the event title doesn’t show up.
So here’s the final piece of code that adds a local calendar to the Android emulator:
ContentValues vals = new ContentValues();
vals.put("_id", 1);
vals.put("_sync_account", account.name);
vals.put("_sync_account_type", account.type);
vals.put("name", account.name);
vals.put("displayName", account.name);
vals.put("color", 14417920);
vals.put("access_level", 700);
vals.put("selected", 1);
vals.put("ownerAccount", account.name);
vals.put("sync_events", 1);
vals.put("timezone", "GMT");
vals.put("hidden", 0);
getContentResolver().insert(calUri, vals);
Where calUri is something like
content://com.android.calendar/calendars?caller_is_syncadapter=true&account_name=local&account_type=LOCAL
and account is an android.accounts.Account.
I’ve put the code into a small application that reads the available accounts and creates a local calendar via a simple click on a button.
Download
- AddLocalCalendar.apk (incl. source).
Attention: Your emulator has to run Google APIs (Google Inc.) – API Level 8 and not Android 2.2 – API Level 8.
Thanks – works a treat! I have a wm8605 netbook that arrived with a broken Android installation.. no calendar sync; I cant install the apk because I need root, and I cannot root because the USB drivers dont work! All I want to do is get the Calendar app working, and your blog post got me there.
Hurray! Thx.
Hello Stefan,
I’m new to android development, and started my experience with developing an application to help manage the Android’s calendar app.
Thank you for the sharing – this is what I’m looking for.
My implementation is much similar to yours.
However, I cannot delete a local calendar created by the application.
using the same content Uri + the query parameters seems not to do the work.
Did you get the chance to extend your researches regarding also deletions?
If you have some insites, I’d like very much to be shared.
Sincerely,
Shilo
Hello Shilo. As far as I know, I was able to delete the local calendar. I would have to take a look at my sourcecode to find out how I did it. If I find the time to look it up, I’ll inform you.
Hello Stefan,
I do not speak very good English but I try to explain my problem.
I’m trying to create a calendar in my application following your code. And when I create a calendar I get the following error message:
Error while creating calendar
the name must no be empty: null
What happens? I need to add anything?
Thanks!
Hi Jesus. Did you run my APK or did you just use the code snippet above? If the latter is the case, please take a look at the complete source code in the APK.
hello stefan
I used your code but i got one error always please check and reply fast.
Error while creating calendar
the name must no be empty: null
Thank you.This solves the problem elegantly.Nice piece of code.
I had the same problem as Jesus (no pun intended) and Narayan above, building from source (whole project).
I think it is because of differences between API Level 8 used by Stefan and my own API Level 14: calendar column names have changed.
I solved it like this:
For some reason, all the code I posted got base64-scribbled. I’ll try again without the “code” block:
import android.provider.CalendarContract.Calendars;
…
vals.put(Calendars.NAME, account.name);
vals.put(Calendars.ACCOUNT_NAME, account.name);
vals.put(Calendars.ACCOUNT_TYPE, account.type);
vals.put(Calendars.CALENDAR_DISPLAY_NAME, “Local Calendar”);
vals.put(Calendars.CALENDAR_COLOR, 14417920);
vals.put(Calendars.CALENDAR_ACCESS_LEVEL, 700);
vals.put(Calendars.VISIBLE, 1);
vals.put(Calendars.OWNER_ACCOUNT, account.name);
vals.put(Calendars.SYNC_EVENTS, 1);
vals.put(Calendars.CALENDAR_TIME_ZONE, “GMT”);
Pingback:Using local calendar in emulator | BlogoSfera
Pingback:Using local calendar in emulator - How-To Video