sam88 sam88 - 3 months ago 6
Android Question

After picking date and time from Date & Time Picker, how to pass them as parameters in 'Create Event' function of Calendars?

I have a Date & Time Picker widget of Android Studio, from which i have selected a particular date(dd-mm-yy) format and time(hr:min) format.

Now, in the same activity i have a 'Submit' button;by clicking on it, i pass several parameters to the 'Create Event' of my phone's native Calendar.

While i can successfully pass the title,description, location etc. i don't know how to pass the 'Start-End Date' & 'Start-End' Time parameters that i have chosen from the Date & Time Picker to the Native Calendar.

Thanks.

Here, is a brief code of the same:

1> Selecting date function:

private void selectDate()
{
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dpd = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener()
{

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
Toast.makeText(getActivity(), dayOfMonth + "-" + (monthOfYear + 1) + "-" + year, Toast.LENGTH_LONG).show();
}
}, mYear, mMonth, mDay);
dpd.show();
}


2> Selecting Time function:

private void selectTime()
{
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

TimePickerDialog tpd = new TimePickerDialog(getActivity(),
new TimePickerDialog.OnTimeSetListener()
{

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
Toast.makeText(getActivity(), hourOfDay + ":" + minute, Toast.LENGTH_LONG).show();
}
}, mHour, mMinute, false);
tpd.show();
}


3) On Submit button click:

Sb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent schedule = new Intent(Intent.ACTION_INSERT);
schedule.setType("vnd.android.cursor.item/event");
schedule.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime);
schedule.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime);
schedule.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, false);
schedule.putExtra(CalendarContract.Events.TITLE, "Title");
schedule.putExtra(CalendarContract.Events.EVENT_LOCATION, "Location");
startActivity(schedule);
}
});


I have edited my code to accomodate a suggestion:

private void selectTime()
{
TimePickerDialog tpd = new TimePickerDialog(getActivity(),
new TimePickerDialog.OnTimeSetListener()
{

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);

Toast.makeText(getActivity(), hourOfDay + ":" + minute, Toast.LENGTH_LONG).show();
}
}, mHour, mMinute, false);
tpd.show();
}


Now, the time perfectly changes/updates every time i create a new event! However, someone plz guide me, what code should be written to change the date of the calendar. Thanks

Answer

You could make a Calendar instance variable:

Calendar calendar = Calendar.getInstance();

Set its values from onDateSet() and onTimeSet():

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    calendar.set(year, monthOfYear, dayOfMonth);
}

// ...

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
    calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calendar.set(Calendar.MINUTE, minute);
}

And then pass the value of this Calendar instance as millis from epoch:

schedule.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
    calendar.getTimeInMillis());
Comments