2

I am very much surprised that I couldn't find this in net even after 1 hour searching!

I have a date picker dialog slightly modified from the original Pickers example (thanks to Raghunandan for this).

But I do not want to set the default date as current date, rather it should pick the date from EditText and set that date to datepickerdialog. Can anybody please help me solving this? My current code stands as below.

EDITED I need to know how I can access the value of EditText datepurchased from inside DatePickerFragment class.

MAIN CLASS:

public class EditEntry extends Activity  implements DatePickerFragment.TheListener{
    EditText edit_datepurchased;
public void showdate(View v) {
        edit_datepurchased = (EditText) findViewById(v.getId());
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }
public void returnDate(String date) {
        edit_datepurchased.setText(date);
    }
}

DatePickerFragment CLASS:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    TheListener listener;

    public interface TheListener{
        public void returnDate(String date);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){

        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);
        listener = (TheListener) getActivity(); 
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day){
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(c.getTime());
        if (listener != null) 
        {
          listener.returnDate(formattedDate); 

        }
          //listener.returnDate(year+"-"+(month+1)+"-"+day);
    }
}

n.b.: I have a question here on same code before, but I don't know how to add question in old thread.

Community
  • 1
  • 1
abdfahim
  • 2,523
  • 10
  • 35
  • 71

3 Answers3

7

Using SimpleDateFormat you can convert the date in the edittext to milliseconds.

example:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy",Locale.US);

you will need to adjust the format based on how you are displaying it but after that just use the parse method from the DateFormat object

Date d = df.parse(dateString);

then create a Calendar object and set the date

Calendar calendar = Calendar.getInstance();
calendar.setDate(d);

Edit:

to send the text from the EditText you need to send the string in the bundle when you create the dialogfragment

example:

DialogFragment newFragment = new DatePickerFragment();
Bundle bundle = new Bundle();
bundle.putString("dateAsText",edit_datepurchased.getText().toString());
newFragment.setArguments(bundle); 
newFragment.show(getFragmentManager(), "datePicker");

then in your dialogfragment use getArguments() to get the bundle

Bundle bundle = getArguments();
String date = bundle.getString("dateAsText");
abdfahim
  • 2,523
  • 10
  • 35
  • 71
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • thanks for that, but I am mainly stuck at how to get the value of EditText datepurchased inside DatePickerFragment class ... "EditText a = (EditText) findViewById(R.id.datepurchased);" gives error (The method findViewById(int) is undefined for the type DatePickerFragment) – abdfahim Nov 11 '13 at 15:29
  • you dont, you would have to send it in the `Bundle` when you create your fragment. see my edit – tyczj Nov 11 '13 at 15:41
  • sorry for delay answering, and thanks a lot for this perfect solution ... if you kindly help with another thing ..calendar.setDate(d) is not recognizable, telling The method setDate(Date) is undefined for the type Calendar ... – abdfahim Nov 11 '13 at 16:46
  • 1
    oh sorry its `setTime` not setDate – tyczj Nov 11 '13 at 16:47
  • thanks ... and can you please also tell me why SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date d = df.parse("2008-11-12"); is giving "Unhandled exception type ParseException" error on df.parse? – abdfahim Nov 11 '13 at 17:02
  • 1
    you need to surround it with a try/catch – tyczj Nov 11 '13 at 17:04
  • setDate is depreciated! – Naveed Ahmad Jun 06 '14 at 10:08
2

I hope it helps you:

public class DatePickerFragment extends DialogFragment
{
    private OnDateSetListener onDateSetListener;

    public DatePickerFragment() {}

    public void setOnDateSetListener(OnDateSetListener onDateSetListener) {
        this.onDateSetListener = 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(), onDateSetListener, year, month, day);
    }

}
NickF
  • 5,637
  • 12
  • 44
  • 75
  • So you down vote me to promote your answer. That behavior is not appropriated. – NickF Nov 11 '13 at 15:12
  • 2
    I downvote you because your answer does not answer his question and just copies what he already has with little explanation on why your answer would be correct if it were – tyczj Nov 11 '13 at 15:18
1

"final Calendar c=Calendar.getInstance();" ,This code set "c" to current time,modify it to what time you want.

LL520
  • 159
  • 3