Search code examples
androiddialogandroid-fragmentsdatetimepicker

Show TimePickerDialog from a Fragment


I searched the web and I found a useful code which handles and manages TimePickerDialog like this one

http://androidexample.com/Time_Picker_With_AM_PM_Values_-_Android_Example/index.php?view=article_discription&aid=86&aaid=109

I did tried it and it works but when I integrate it on my class which extends to fragment, TimePickerDialog is not showing on button click

Do you have any idea how can I implement this one?

public class First_Activity extends Fragment implements ActionBar.TabListener {   
 static final int TIME_DIALOG_ID = 1111;
    public Button btnClick;
    TextView STtime;
    int hour,minute; 

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


STtime = (TextView) getActivity().findViewById(R.id.textView2);


        /********* display current time on screen Start ********/

           final Calendar c = Calendar.getInstance();
           // Current Hour
           hour = c.get(Calendar.HOUR_OF_DAY);
           // Current Minute
           minute = c.get(Calendar.MINUTE);

           // set current time into output textview
           updateTime(hour, minute);

        /********* display current time on screen End ********/

btnClick = (Button) getActivity().findViewById(R.id.button4);
        btnClick.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                getActivity().showDialog(TIME_DIALOG_ID);

            }
        });
}

 protected Dialog onCreateDialog(int id) {
           switch (id) {
           case TIME_DIALOG_ID:

               // set time picker as current time
               return new TimePickerDialog(getActivity(), timePickerListener, hour, minute,
                       false);

           }
           return null;
       }

private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {


           @Override
           public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
               // TODO Auto-generated method stub
               hour   = hourOfDay;
               minute = minutes;

               updateTime(hour,minute);

            }

       };



private static String utilTime(int value) {

           if (value < 10)
               return "0" + String.valueOf(value);
           else
               return String.valueOf(value);
       }

 private void updateTime(int hours, int mins) {

       String timeSet = "";
       if (hours > 12) {
           hours -= 12;
           timeSet = "PM";
       } else if (hours == 0) {
           hours += 12;
           timeSet = "AM";
       } else if (hours == 12)
           timeSet = "PM";
       else
           timeSet = "AM";


       String minutes = "";
       if (mins < 10)
           minutes = "0" + mins;
       else
           minutes = String.valueOf(mins);

       // Append in a StringBuilder
        String aTime = new StringBuilder().append(hours).append(':')
               .append(minutes).append(" ").append(timeSet).toString();

         STtime.setText(aTime);
   }    
}

Solution

  • I would recommend that you create a layout file for your Frament looking like this:

    your_custom_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent" 
                  android:layout_height="fill_parent" 
                  android:orientation="vertical" >
        <Button android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Spawn Dialog" />
    </LinearLayout>
    

    And a custom Fragment looking like this:

    public class MyFragment extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.your_custom_layout, container, false);
    
            Button b = (Button) v.findViewById(R.id.button);
            b.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    createDialog(YOUR_ID).show();
                }
            });
    
            return v;
        }
    
        public Dialog createDialog(int id) {
               switch (id) {
               case TIME_DIALOG_ID:
    
                   // set time picker as current time
                   return new TimePickerDialog(getActivity(), timePickerListener, hour, minute, false);
    
               }
               return null;
           }
    }
    

    So you have a Fragment whith a layout that contains a button, when clicking the button, the Dialog spawns.