Raghav Kumar
Raghav Kumar

Reputation: 489

Android - Date Picker - Obatin Value

I am using a DatePicker Dialog in my activity for the user to select a Date. What I want to do is to display the selected date in a textView. Can anyone please help me on how to obtain the selected date and then display it accordingly. Thanks in advance!

Following is the code i'm using to implement the DatePicker Dialog:

public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

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

            public void onDateSet(DatePicker view, int yyyy, int mm, int dd) {
            // Do something with the date chosen by the user


            }
}

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
}   

Upvotes: 1

Views: 729

Answers (2)

Herrbert74
Herrbert74

Reputation: 2715

Implement a Listener Interface in your activity and pass it to the DatePickerFragment. Then trigger the listener by calling it's methods. It's described here: http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents

Upvotes: 0

user2251725
user2251725

Reputation: 406

in onDateSet method,

String cuurent_date = String.valueOf(c.get(Calendar.DAY_OF_MONTH))
            + "/" + String.valueOf(c.get(Calendar.MONTH) + 1) + "/"
            + String.valueOf(c.get(Calendar.YEAR));

Upvotes: 1

Related Questions