I am gonna explain that in steps.
Step 1:- Create Timepicker and DatePicker
Step 2:- Set ClickListener on some Button or edittext to launch timepicker and datepicker
Step 3:- Declare interface in TimepickerFragment and DatepickerFragment (OnTimeChangeListenerInterface and OnDateChangeListenerInterface in the given example)
Step 4:- Implement the above interfaces in your MainActivity and override the onTimeChangeListener and onDateChangeListener methods to receive the selected time and date.
1. MainActivity.java
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements
TimePickerFragment.OnTimeChangeListenerInterface,
DatePickerFragment.OnDateChangeListenerInterface{
private EditText timeEditText;
private EditText dateEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
timeEditText = (EditText) findViewById(R.id.time_edit_text);
dateEditText = (EditText) findViewById(R.id.date_edit_text);
timeEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment timePickerFragment = new TimePickerFragment();
timePickerFragment.show(getSupportFragmentManager(), "timePicker");
}
});
dateEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePickerFragment = new DatePickerFragment();
datePickerFragment.show(getSupportFragmentManager(), "datePicker");
}
});
// method from OnTimeChangeListenerInterface, this method will be called when user select time from time picker
@Override
public void onTimeChangeListener(String time) {
timeEditText.setText(time); // set time to the edittext
}
// method from OnDateChangeListenerInterface, this method will be called when user select the date
@Override
public void onDateChangeListener(String date) {
dateEditText.setText(date); // set date to edittext
}
}
}
xml file:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="50dp"
tools:context="ad.abc.com.datetimepicker.MainActivity">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:id="@+id/time_edit_text"
android:focusable="false"
android:hint="Time" />
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/date_edit_text"
android:focusable="false"
android:hint="Date"/>
</LinearLayout>
Clicking on timeEditText and DateEditText will launch time-picker and date-picker respectively. Code for timepicker and datepicker fragment is in next step.
2. Code for TimePicker
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;
import java.util.Calendar;
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
// declaration of interface, need to be implemented in MainActivity
public interface OnTimeChangeListenerInterface{
void onTimeChangeListener(String time); // this method needs to be override in MainActivity to get the value of selected time
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
// callback to the onTimeChangeListener method in MainActivity to pass value of selected time
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// passing selected value to the MainActivity through onTimeChangeListener method
((OnTimeChangeListenerInterface) getActivity()).onTimeChangeListener(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
}
}
Similarly, for DatePicker
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
public interface OnDateChangeListenerInterface{
void onDateChangeListener(String date);
}
@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 onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
// passing selected value to the MainActivity through onDateChangeListener method
((OnDateChangeListenerInterface) getActivity()).onDateChangeListener(String.valueOf(dayOfMonth) + "-" + String.valueOf(month) + "-" + String.valueOf(year));
}
}