How to Create a Date and Time Picker in Android

How to create a date and time picker in Android?

There is nothing built into Android that offers this.

EDIT: Andriod now offers built-in pickers. Check @Oded answer

Jetpack compose. Date time picker

Using a library

As of writing, It's not implemented yet. But there is an open-source library Compose Material Dialogs

Date and Time Picker

Without a library

You can Also do this without a library, you can put it inside onclick action

private fun showDatePicker() {
val picker = MaterialDatePicker.Builder.datePicker().build()
activity?.let {
picker.show(it.supportFragmentManager, picker.toString())
picker.addOnPositiveButtonClickListener {

}
}
}

The old way :D

DateTimePicker android

Try this:

MainActivity.java

public class MainActivity extends AppCompatActivity implements
DatePickerFragment.DatePickerListener, TimePickerFragment.TimePickerListener
{
private Calendar calendar;
FragmentManager fm;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

calendar = Calendar.getInstance();

fm = getSupportFragmentManager();
DatePickerFragment dateDialog = new DatePickerFragment();
dateDialog.show(fm, "fragment_date");
}

@Override
public void onDateSet(int year, int month, int day)
{
// Set selected year, month and day in calendar object
calendar.set(year, month, day);

// Start Time dialog
TimePickerFragment timeDialog = new TimePickerFragment();
timeDialog.show(fm, "fragment_time");

}

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

// Here, your calendar object is ready.
}
}

DatePickerFragment.java

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
public interface DatePickerListener
{
public void onDateSet(int year, int month, int day);
}

private DatePickerListener listener;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);

// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
listener = (DatePickerListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}

public void onDateSet(DatePicker view, int year, int month, int day)
{
listener.onDateSet(year, month, day);
}
}

TimePickerFragment.java

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener
{
public interface TimePickerListener
{
public void onTimeSet(int hourOfDay, int minute);
}

private TimePickerListener listener;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Calendar c=Calendar.getInstance();
int hour=c.get(Calendar.HOUR_OF_DAY);
int minute=c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(),this,hour,minute, DateFormat.is24HourFormat(getActivity()));
}

@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);

// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
listener = (TimePickerListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
listener.onTimeSet(hourOfDay, minute);
}
}

Hope this will solve your problem.

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

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());

Date & Time Picker Dialog

set(int,int) in Calendar takes in the field id in its first argument and value in second. To set hour and minute, split it to two calls, e.g.

startCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay)
startCalendar.set(Calendar.MINUTE, minute)

(Also, consider using java.time APIs instead. They are much nicer to work with.)

Is there an option to change Date Picker form?

check that library: https://github.com/florent37/SingleDateAndTimePicker

I've used it some time ago and it did almost exactly what you're looking for ;)

If it doesn't fit your needs, you can look for more 'iOS style date pickers' for Android, for example in this SO post:

ios like date/time picker for android platform

Edit:

It seems that this picker is Holo Light style and is achievable with this piece of code:

DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_LIGHT,this,year,month,day);

But you need to check that on your own because they might be deprecated or not working with other themes.



Related Topics



Leave a reply



Submit