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

3 thoughts on “How to Display TimePickerDialog inside a Fragment

  1. you are genius man!
    I have searched a lot onto this topic,displaying TimePickerDialog in fragment class,similarly the way you have showed above.But, I have always encountered with lengthy of codes which i myself didn’t wanted to implement.But, before giving up with this thing,I somehow visited this site and found this two line codes which was seriously very simple and i got what you did.You just removeed the method that is undefined in fragment class and whatever single line is written there you have used that directly.Seriously,quite simple 🙂

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s