user2286970
user2286970

Reputation: 31

Create modal datepicker dialog in Android

I have this code that works fine, but the window is not modal, so I can't get the date after it is changed. Please give me some advice or approach about it, thanks in advance.

Here is the code:

final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dpd = new DatePickerDialog(this,
       new DatePickerDialog.OnDateSetListener() {

           public void onDateSet(DatePicker view, int year,
                                 int monthOfYear, int dayOfMonth) {

               TextView txtDate = null;
               txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

           }

           public void onDateSet1(DatePicker view, int year,
                                  int monthOfYear, int dayOfMonth) { // TODO
               // Auto-generated
               // method stub

           }
       }, mYear, mMonth, mDay);

dpd.show();

message("message", "Mensaje", mYear + "/" + mMonth + "/" + mDay, this, context);

Upvotes: 1

Views: 1350

Answers (1)

tiny sunlight
tiny sunlight

Reputation: 6251

private int storeYear = 0;
private int storeMonth = 0;
private int storeDay = 0;

private void  function openPicker(){
   final TextView txtDate  = findViewById(R.id.yourid);
  final Calendar c = Calendar.getInstance();
    int mYear = c.get(Calendar.YEAR);
    int mMonth = c.get(Calendar.MONTH);
    int mDay = c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dpd = new DatePickerDialog(this,
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year,
                        int monthOfYear, int dayOfMonth) {

                   //TextView txtDate = null;
                    txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1)
                            + "-" + year);
                    storeYear = year;storeMonth = monthOfYear;storeDay = dayOfMonth;

    message("message", "Mensaje", mYear + "/" + mMonth + "/"
            + mDay, this, context);
                }

                public void onDateSet1(DatePicker view, int year,
                        int monthOfYear, int dayOfMonth) { // TODO
                                                            // Auto-generated
                                                            // method stub

                }
            }, mYear, mMonth, mDay);
    dpd.show();

}

Upvotes: 1

Related Questions