-1

How to set a Timepicker on EditText Click in android.

I have searched the following links but it doesn't work as the showDialog() is now depricated.

TimePicker Dialog from clicking EditText

http://www.botskool.com/geeks/how-create-time-picker-dialog-selecting-time-android

any help would be acknowledge.

Community
  • 1
  • 1
Pravinsingh Waghela
  • 2,516
  • 4
  • 32
  • 50

3 Answers3

1

Use this method for time picker android developers site

    public static class TimePickerFragment extends DialogFragment
                            implements TimePickerDialog.OnTimeSetListener {

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

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
    }
}

and to call it you can do it like this

<EditText
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:text="@string/pick_time" 
android:onClick="showTimePickerDialog" />

showTimePickerDialog method

    public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");
}
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
0

You have to use TimePickerDialog class to display it as Dialog. Here is sample code:

  TimePickerDialog timePickerDialog = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                }
            }, 0, 0, true);
            timePickerDialog.show();

This code you may call in your OnClickListener method

Volodymyr
  • 6,393
  • 4
  • 53
  • 84
0

I got the answer for the same:-

Include the following methods in your Fragment.

public void selectTimeToDisplay(EditText edText) {
        DialogFragment dialogFragment = new DisplayTimeFragment(edText);
        dialogFragment.show(getActivity().getSupportFragmentManager(), "DatePicker");
    }

public void setSelectedTime(String hourOfDay,String minute, EditText edText) {
    edText.setText(hourOfDay + ":" + minute);
}

Define DisplayTimeFragment as a Inner class in your Fragment:-

public class DisplayTimeFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
        EditText edText;
        public DisplayTimeFragment(EditText edText){
            this.edText = edText;
        }

        @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);
            return new TimePickerDialog(getActivity(), this, hour, minute,
                    DateFormat.is24HourFormat(getActivity()));
        }
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            String sHrs, sMins;
            if(minute<10){
                sMins = "0"+minute;
            }else{
                sMins = String.valueOf(minute);
            }
            if(hourOfDay<10){
                sHrs = "0"+hourOfDay;
            }else{
                sHrs = String.valueOf(hourOfDay);
            }
            setSelectedTime(sHrs,sMins, edText);

        }
    }

Now, Write the following code in the onClickListener of your Edittext.

as,

edStartTime.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                selectTimeToDisplay(edStartTime);
            }
        });

Note:- For Smooth functioning, Set focusable to false in the xml of the Edittext.

Bingo.. It will work as charms for any no of EditText now...

Pravinsingh Waghela
  • 2,516
  • 4
  • 32
  • 50