Weal Infotech Weal Infotech - 4 years ago 132
Android Question

How to set 2 timepicker in 2 different EditText?

I want to set two

in Two
but when i select any Time Picker it will display the same time in Both

for. ex: when i select
then it will set automatically in
(EditText) too.

Here the code :-


starttime.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Initialize a new time picker dialog fragment
DialogFragment dFragment = new TimePickerFragment();

// Show the time picker dialog fragment
dFragment.show(getActivity().getFragmentManager(),"Time Picker");
isFromClicked = false;
endtime.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Initialize a new time picker dialog fragment
DialogFragment dFragment1 = new TimePickerFragment();

// Show the time picker dialog fragment
dFragment1.show(getActivity().getFragmentManager(),"Time Picker");
isFromClicked = true;



public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{

public Dialog onCreateDialog(Bundle savedInstanceState){
// Get a Calendar instance
final Calendar calendar = Calendar.getInstance();
// Get the current hour and minute
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);

// TimePickerDialog Theme : THEME_DEVICE_DEFAULT_LIGHT
TimePickerDialog tpd = new TimePickerDialog(getActivity(),

// Return the TimePickerDialog
return tpd;

public void onTimeSet(TimePicker view, int hourOfDay, int minute){

String am_pm = "";

Calendar datetime = Calendar.getInstance();
datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
datetime.set(Calendar.MINUTE, minute);

if (datetime.get(Calendar.AM_PM) == Calendar.AM)
am_pm = "AM";
else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
am_pm = "PM";

String strHrsToShow = (datetime.get(Calendar.HOUR) == 0) ?"12":datetime.get(Calendar.HOUR)+"";

((EditText)getActivity().findViewById(R.id.start_time)).setText( strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm );

((EditText)getActivity().findViewById(R.id.End_time)).setText( strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm);


Answer Source

Use this class for time picker. I have used it and it works for 2 editexts

    public class TimePicker extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
    EditText text;
    Context context;
    private TimePickerDialog timePickerDialog;

    public TimePicker(EditText text, Context context) {
        this.text= text;
        this.context = context;

    public TimePicker() {

    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,

    public void onTimeSet(android.widget.TimePicker view, int hourOfDay, int minute) {
        Time t = new Time(hourOfDay,minute,0);//seconds by default set to zero
        Format formatter;
        formatter = new SimpleDateFormat("h:mm a", Locale.ENGLISH);
        String time = formatter.format(t);



TimePicker timePicker = new TimePicker(editTextName, getActivity());
timePicker.show(getActivity().getSupportFragmentManager(), "Start Time");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download