Question

I am trying to show selected date in datepicker dialog when select again.

Here is what i have tried.

 signup.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


             Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog mDatePicker=new DatePickerDialog(CreateAccountActivity.this, new OnDateSetListener()
                {   
                    @Override
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) 
                    {
                            year = selectedyear; 
                            month = selectedmonth; 
                            day = selectedday;
                     signup.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                    }
                },year, month, day);
                mDatePicker.setTitle("Please select date");                
                mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
                mDatePicker.show();


        }
    });

But problem is this when i again selects my date . it shows current date. and i want to show selected date in dialog.

Was it helpful?

Solution

i have solved the answer ...

here is the answer....

public void onClick(View v) {

             if (year == 0 || month == 0 || day == 0) {
             Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
             }

                DatePickerDialog mDatePicker=new DatePickerDialog(CreateAccountActivity.this, new OnDateSetListener()
                {   
                    @Override
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) 
                    {
                            year = selectedyear; 
                            month = selectedmonth; 
                            day = selectedday;
                     signup.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                    }
                },year, month, day);
                mDatePicker.setTitle("Please select date");                
                mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
                mDatePicker.show();

i followed the page DatePickerDialog always opens the current date

and read comment and thanks @Andrew T :)

OTHER TIPS

you can use this:

private EditText mbirthdayView;  
    mbirthdayView=(EditText)findViewById(R.id.birthday);
mbirthdayView.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDialog(DATE_DIALOG_ID);
        }
    });

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
            return new DatePickerDialog(this, datePickerListener, year, month,
                    day);
        }
        return null;
    }

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) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // set selected date into textview
            mbirthdayView.setText(  (day)+(".")+(month + 1)+(".")+(year));

            // set selected date into datepicker also
            dpResult.init(year, month, day, null);

        }
    };

Yon can follow my example :

Open “res/layout/main.xml” file, add date picker, label and button for demonstration.

<?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/btnChangeDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Date" />

    <TextView
        android:id="@+id/lblDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Current Date (M-D-YYYY): "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <DatePicker
        android:id="@+id/dpResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Read the code’s comment, it should be self-explanatory.

File : MyAndroidAppActivity.java

public class MyAndroidAppActivity extends Activity {

    private TextView tvDisplayDate;
    private DatePicker dpResult;
    private Button btnChangeDate;

    private int year;
    private int month;
    private int day;

    static final int DATE_DIALOG_ID = 999;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setCurrentDateOnView();
        addListenerOnButton();

    }

    // display current date
    public void setCurrentDateOnView() {

        tvDisplayDate = (TextView) findViewById(R.id.tvDate);
        dpResult = (DatePicker) findViewById(R.id.dpResult);

        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);

        // set current date into textview
        tvDisplayDate.setText(new StringBuilder()
            // Month is 0 based, just add 1
            .append(month + 1).append("-").append(day).append("-")
            .append(year).append(" "));

        // set current date into datepicker
        dpResult.init(year, month, day, null);

    }

    public void addListenerOnButton() {

        btnChangeDate = (Button) findViewById(R.id.btnChangeDate);

        btnChangeDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                showDialog(DATE_DIALOG_ID);

            }

        });

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
           // set date picker as current date
           return new DatePickerDialog(this, datePickerListener, 
                         year, month,day);
        }
        return null;
    }

    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) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // set selected date into textview
            tvDisplayDate.setText(new StringBuilder().append(month + 1)
               .append("-").append(day).append("-").append(year)
               .append(" "));

            // set selected date into datepicker also
            dpResult.init(year, month, day, null);

        }
    };

}

I expect you will be helpful from my example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top