How to Display TimePickerDialog inside a Fragment

A developer innovates, design, invent new things only there occurs a definite need. In my project I came across a need that, “I need to display time picker dialog when I click a view say (edittext) inside the fragment”. I done a small research on this, later I found a simple solution.

Scenario – Overcome

1) There is an edittext view inside a fragment I named it to Fajar, and implemented onClickListener for this view.

2) Inside the onClick method of Fajar,

Simple just Create an object of android.app.TimePickerDialog and give the respective parameters to it, in general it looks for this parameters

android.app.TimePickerDialog.TimePickerDialog(Context context, OnTimeSetListenercallBack, int hourOfDay, int minute, boolean is24HourView)

I gave simple parameters to the object of TimePickerDialog, and at last dialog was shown by this single line,

new TimePickerDialog(getActivity(), this, 0, 0, false).show();

You may judge that I made the fragment class to implement OnTimeSetListener, as a result Image trick at Overrided method onTimeSet() to show the respective time setted / selected from dialog.

Here it is,

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
fajar.setText(String.valueOf(hourOfDay)+"Hours "+String.valueOf(minute)+" minutes ");
}

For more clarity I added screen shots too

Image

Screen no. 1

Image

Screen 2

Image

Screen 3

But I found alternate solutions to this and I had not implemented here since I made the job done in easier way.

Enjoy ..!

Advertisements