Creating Calendar Events – Using WebView – in Android

Creating Calendar Events – Using WebView – in Android

Hey – it’s been a while.

No time like the present to write a post about Android coding.

Specifically: “how do I create a calendar event from within a WebView in Android?”

First, let’s talk game plan. I am going to construct an url “scheme” that will indicate to a handler that I want to create a calendar event, and not navigate to a web page; then, I need to call an Intent that will allow the event to be created.

Easy Peasy.

So, for my dates that I wish to portray on a web view, I do something like the following:

<a href='date:beginmonth, beginday, beginyear, 
endmonth, endday, endyear, My Event Description'>
My event link</a>

Then, I construct a shouldOverrideUrlLoading handler that will detect that a date: is being clicked, and handle it specially (see the code below):

final WebView crimeView = (WebView) findViewById(R.id.webview_crime);

crimeView.setWebViewClient(new WebViewClient() {

@Override

public boolean shouldOverrideUrlLoading (WebView view, String url)

{

if (url.startsWith("date:")) {

Log.d(this.getClass().getCanonicalName(),url);

Calendar beginCal = Calendar.getInstance();

Calendar endCal = Calendar.getInstance();

Date beginDate = new Date(0, 0, 0);

Date endDate = new Date(0, 0, 0);

String parsed = url.substring(5);

String[] components = parsed.split(",");

beginDate.setMonth(Integer.parseInt(components[0]));

beginDate.setDate(Integer.parseInt(components[1]));

beginDate.setYear(Integer.parseInt(components[2]));

beginCal.setTime(beginDate);

endDate.setMonth(Integer.parseInt(components[3]));

endDate.setDate(Integer.parseInt(components[4]));

endDate.setYear(Integer.parseInt(components[5]));

endCal.setTime(endDate);

calendarevent(beginCal, endCal, components[6]);

return true;

}

return false;

}

});

Finally, we need code to invoke the Calendar intent. It looks like the following:

 // Create a calendar event
    public void calendarevent(Calendar begintime, Calendar endtime, 
                              String eventName) 
    {
        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("beginTime", begintime.getTimeInMillis());
        intent.putExtra("allDay", false);
        intent.putExtra("rrule", "FREQ=YEARLY");
        intent.putExtra("endTime", endtime.getTimeInMillis()+60*60*1000);
        intent.putExtra("title", eventName);
        startActivity(intent);
    }

And that, as they say, is that.

Advertisements

2 thoughts on “Creating Calendar Events – Using WebView – in Android

  1. Hi David, nice post about webview and calendar events, but I tried to apply your method to the app I’m developing and some of the code is said to be deprecated. Any chance you know a new way of doing so? Cause it seems no one knows how to do it anymore, android sdk has some dark sides there..
    Anyway, if you can help me with this I’d appreciate it so much!

    Like

  2. Thanks for sharing the useful example. I have question about including the start time and end time in the calendar event, what should I do?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s