Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create datePicker and timePicker dialogs in fragment class?

Tags:

android

I want to know is there a way to create a datePicker in a fragment? I am creating one the regular activity may and it gives me syntax error. What is the correct way to do this?

like image 859
yoshi24 Avatar asked Jul 12 '11 17:07

yoshi24


3 Answers

Expanding on Codejoy's answer:

Here is my DatePickerDialogFragment class:

public class DatePickerDialogFragment extends DialogFragment {
    private Fragment mFragment;

    public DatePickerDialogFragment(Fragment callback) {
        mFragment = callback;
    }

     public Dialog onCreateDialog(Bundle savedInstanceState) {
         return new DatePickerDialog(getActivity(), (OnDateSetListener) mFragment, 1980, 7, 16);
     }
}

(Notice that the constructor accepts the fragment that is using this dialog - and that we use this reference for the callback listener field for DatePickerDialog)

My fragment then just implements onDateSetListener:

public class SignupFragment extends Fragment implements OnDateSetListener {
...
public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
    // do stuff with the date the user selected
}
}

... and then I show the dialog from my Fragment like so:

FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = new DatePickerDialogFragment(this);
newFragment.show(ft, "dialog");

Not sure if this is the best way to do it, but seems to work fine.

like image 99
NPike Avatar answered Oct 16 '22 00:10

NPike


Though above answers might work, I believe I have a much easier solution:

DatePickerDialog dialog = new DatePickerDialog(getActivity(), datePickerListener, 
                             2000, 1,1);
dialog.show();

Where datePickerListerener is something like this:

private DatePickerDialog.OnDateSetListener datePickerListener 
            = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
            //Do whatever you want
        }
};
like image 35
Bart Burg Avatar answered Oct 15 '22 23:10

Bart Burg


Previous anwsers are explaining good enough but here is how I changed the view onDateSet method of DialogFragment.

You can pass the view you want to change to the constructer of class extending DialogFragment. Then call setText method onDateSet method of the class. Here is the code:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

public EditText activity_edittext;

public DatePickerFragment(EditText edit_text) {
    activity_edittext = edit_text;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
}

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        activity_edittext.setText(String.valueOf(month + 1 ) + "/" + String.valueOf(day) + "/" + String.valueOf(year));
    }
}

Then you can show your fragment like this:

public void showDatePickerDialog(View v) {
    new DatePickerFragment((EditText) v).show(getSupportFragmentManager(), "datePicker");
}

Set this method onClick method of EditText you want to change. Like this:

    <EditText
            android:id="@+id/editTextToShowDate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:focusable="false"
            android:inputType="date"
            android:onClick="showDatePickerDialog" >
   </EditText>
like image 27
Kerim Oguzcan Yenidunya Avatar answered Oct 16 '22 00:10

Kerim Oguzcan Yenidunya