Forum Xamarin.Android

Open Add New Event (Calendar) Dialog

Could someone please tell me if it is possible to open the Add New Event dialog on Android? All of the examples that I've found only tell how to create and add a new event from code and it skips that screen. Ideally, I would like to be able to open that dialog and prepopulate some of the fields if that is possible. Thanks in advance for the help.

Answers

  • jharrsjharrs USMember ✭✭
    edited September 2015

    I was able to figure this out on my own. I know this is simple but if someone lands here looking for an answer here you go:

    // use Java.Util for date conversion to long
    using Java.Util;
    
    
    
    // variables
    var intent = new Intent(Intent.ActionEdit);
    
    // set intent type
    intent.SetType("vnd.android.cursor.item/event");
    
    // event title
    intent.PutExtra("title", "New Event Title");
    
    // event description
    intent.PutExtra("description", "This is the description of my new event");
    
     // event location (White House)
     intent.PutExtra("eventLocation", "1600 Pennsylvania Ave NW, Washington, DC 20500");    
    
    // start time (Sept 8, 2015 10:00 AM)
    intent.PutExtra("beginTime", GetDateTimeMS ("September", 8, 2015, 10, 0));
    
    // end time (Sept 8, 2015 11:00 AM)
    intent.PutExtra("endTime", GetDateTimeMS ("September", 8, 2015, 11, 0));
    
    // all day event
    //intent.PutExtra("allDay", true);
    
    // repeat rule
    //intent.PutExtra("rrule", "FREQ=YEARLY");
    
    // start intent
    StartActivity(intent);
    
    
    
    // return date in long
    long GetDateTimeMS (int month, int day, int yr, int hr, int min, string ampm = "am")
    {
        // variables
        Calendar c = Calendar.GetInstance (Java.Util.TimeZone.Default);
    
        // month
        c.Set (CalendarField.Month, month - 1);
    
        // day
        c.Set (CalendarField.DayOfMonth, day);
    
        // year
        c.Set (CalendarField.Year, yr);
    
        // hour
        switch (ampm) {
            case "am":
                c.Set (CalendarField.HourOfDay, hr);
                break;
            case "pm":
                c.Set (CalendarField.HourOfDay, hr + 12);
                break;
            default:
                c.Set (CalendarField.HourOfDay, hr);    
                break;
            }
    
        // minute
        c.Set (CalendarField.Minute, min);
    
        // return
        return c.TimeInMillis;
    }
    
    // return date in long
    long GetDateTimeMS (string month, int day, int yr, int hr, int min, string ampm = "am")
    {
        // variables
        Calendar c = Calendar.GetInstance (Java.Util.TimeZone.Default);
    
        // month
        switch (month.ToLower()) {
        case "january":
            c.Set (CalendarField.Month, 0); 
            break;
        case "february":
            c.Set (CalendarField.Month, 1); 
            break;
        case "march":
            c.Set (CalendarField.Month, 2); 
            break;
        case "april":
            c.Set (CalendarField.Month, 3); 
            break;
        case "may":
            c.Set (CalendarField.Month, 4); 
            break;
        case "june":
            c.Set (CalendarField.Month, 5); 
            break;
        case "july":
            c.Set (CalendarField.Month, 6); 
            break;
        case "august":
            c.Set (CalendarField.Month, 7); 
            break;
        case "september":
            c.Set (CalendarField.Month, 8); 
            break;
        case "october":
            c.Set (CalendarField.Month, 9); 
            break;
        case "november":
            c.Set (CalendarField.Month, 10);    
            break;
        case "december":
            c.Set (CalendarField.Month, 11);    
            break;          
        }               
    
        // day
        c.Set (CalendarField.DayOfMonth, day);
    
        // year
        c.Set (CalendarField.Year, yr);
    
        // hour
        switch (ampm) {
            case "am":
                c.Set (CalendarField.HourOfDay, hr);
                break;
            case "pm":
                c.Set (CalendarField.HourOfDay, hr + 12);
                break;
            default:
                c.Set (CalendarField.HourOfDay, hr);    
                break;
            }
    
        // minute
        c.Set (CalendarField.Minute, min);
    
        // return
        return c.TimeInMillis;
    }
    
Sign In or Register to comment.