user3663600
user3663600

Reputation: 11

How to update the EditText value from DatePickerDialog

I have an EditText which already has some value and when I click the EditText then it opens the DatePickerDialog. So I can choose some date. Once I have chosen the date and clicked done then the previous value should be updated. But I don't know how to update the value.

Here is my code.

estimate_closedate.setText("2014-09-12");
estimate_closedate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println("datepicker inside");
                DatePickerDialog dpd = new DatePickerDialog(context,
                        mDateSetListener, myear, mmonth, mday);
                dpd.show();
            }
estimate_closedate.setText(nextdate);
private DatePickerDialog.OnDateSetListener mDateSetListener = new 
            DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                    myear=year;
                    mmonth=monthOfYear+1;
                    mday=dayOfMonth;
                    if(mmonth<10)
                    {
                        month = 0 + Integer.toString(mmonth);
                    }
                    else
                    {
                        month = Integer.toString(mmonth);
                    }
                    if(mday<10)
                    {
                        day = 0 + Integer.toString(mday);
                    }
                    else
                    {
                        day = Integer.toString(mday);
                    }
                    nextdate = (new StringBuilder().append(myear).append("-").append(month).append("-").append(day)).toString();

                }
            };

Upvotes: 0

Views: 550

Answers (4)

Yakiv Mospan
Yakiv Mospan

Reputation: 8224

You forgot to update EditText in onDateSet method, just put this lines to the end of it :

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    ///....       
    nextdate = (new StringBuilder().append(myear).append("-").append(month).append("").append(day)).toString();

    estimate_closedate.setText(nextdate);//this is how you update edit text
}

Upvotes: 1

Nadeem Iqbal
Nadeem Iqbal

Reputation: 2338

Try this:

 private DatePickerDialog.OnDateSetListener mDateSetListener = new 
        DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                myear=year;
                mmonth=monthOfYear+1;
                mday=dayOfMonth;
                if(mmonth<10)
                {
                    month = 0 + Integer.toString(mmonth);
                }
                else
                {
                    month = Integer.toString(mmonth);
                }
                if(mday<10)
                {
                    day = 0 + Integer.toString(mday);
                }
                else
                {
                    day = Integer.toString(mday);
                }
                nextdate = (new StringBuilder().append(myear).append("-").append(month).append("-").append(day)).toString();

                estimate_closedate.setText(nextdate);
            }
        };

Upvotes: 0

Marija Milosevic
Marija Milosevic

Reputation: 516

Just set the EditText text in your onDateSet method of the date picker. Just after this line:

            nextdate = (new StringBuilder().append(myear).append("-").append(month).append("-").append(day)).toString();

add this:

estimate_closedate.setText(nextdate);

Upvotes: 0

Himanshu Agarwal
Himanshu Agarwal

Reputation: 4691

Try this:

estimate_closedate.setText("2014-09-12");
estimate_closedate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println("datepicker inside");
                DatePickerDialog dpd = new DatePickerDialog(context,
                        mDateSetListener, myear, mmonth, mday);
                dpd.show();
            }

private DatePickerDialog.OnDateSetListener mDateSetListener = new 
            DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                    myear=year;
                    mmonth=monthOfYear+1;
                    mday=dayOfMonth;
                    if(mmonth<10)
                    {
                        month = 0 + Integer.toString(mmonth);
                    }
                    else
                    {
                        month = Integer.toString(mmonth);
                    }
                    if(mday<10)
                    {
                        day = 0 + Integer.toString(mday);
                    }
                    else
                    {
                        day = Integer.toString(mday);
                    }
                    nextdate = (new StringBuilder().append(myear).append("-").append(month).append("-").append(day)).toString();
                    estimate_closedate.setText(nextdate);
                }
            };

Upvotes: 0

Related Questions