4

I am using the following code to create a DatePickerDialog from an EditText.

import android.widget.DatePicker;
import android.app.DatePickerDialog;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

Calendar myCalendar = Calendar.getInstance();

String myFormat = "MM/dd/yy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

@Override
    protected void onCreate(Bundle savedInstanceState) {

EditText datePicker = (EditText) findViewById(R.id.popUpDate);
datePicker.setText(sdf.format(myCalendar.getTime()));

datePicker.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        new DatePickerDialog(AddAccount.this, R.style.AppTheme_Dialog, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                datePicker.setText(sdf.format(myCalendar.getTime()));
            }
        }, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                myCalendar.get(Calendar.DAY_OF_MONTH)).show();
    }
});
}

Is it possible to have the default one automatically close after a date is chosen? Or would that require a complete remake of the datepicker widget?

Charuක
  • 12,953
  • 5
  • 50
  • 88
Leb
  • 15,483
  • 10
  • 56
  • 75

4 Answers4

2

DatePickerDialog containing an DatePicker. And DatePicker have OnDateChangedListener.
You can use OnDateChangedListener to listener when you choose a date like

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

final DatePickerDialog datePickerDialog =
    new DatePickerDialog(YourActivity.this, null, year, month, day);

datePickerDialog.getDatePicker()
    .init(year, month, day, new DatePicker.OnDateChangedListener() {
        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
            Toast.makeText(YourActivity.this, "day" + dayOfMonth + "-month" + monthOfYear + 1 + "-year" + year, Toast.LENGTH_SHORT).show();
            // dismiss the popup when choose a date 
            datePickerDialog.dismiss();
        }
    });
datePickerDialog.show();

This solution will work for your case but I think it is not very good about User experience (UX) because almost Android app use default DatePickerDialog flow

Linh
  • 57,942
  • 23
  • 262
  • 279
1

The Current behavior is fine?

If it will automatically close ...! then how system will know you pick the desired date and time or you accidentally pick it..... there no sense of close it automatically instead of event fire.

But you can close it on onDateSet.

Update: You can close it like this.

// open datepicker dialog.
// set date picker for current date
// add pickerListener listner to date picker

   DatePickerDialog dialog = new DatePickerDialog(this, pickerListener, year, month,day);
   dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Text", new DialogInterface
            .OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_NEGATIVE) {
              // Do Stuff
             Log.i("dialog click", "dialog negative button clicked");
            dialog.dismiss();
            }
          }
        });
   dialog.dismiss();
Leb
  • 15,483
  • 10
  • 56
  • 75
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
  • It will close if you choose a date, if you accidentally chose one it won't be hard to reopen. The idea is you are more likely to choose a correct date than accidentally clicking one. So automatically closing it would be a better route for the overall user. -- The issue is I am not sure what the command to include within the `oneDateSet` – Leb Jan 02 '17 at 13:58
  • are you using android builtin date picker or any other lib? – Sohail Zahid Jan 02 '17 at 14:01
  • [this](http://stackoverflow.com/questions/35104181/how-to-finish-activity-when-date-picker-dialog-on-cancel-button-is-clicked) solution is when the cancel button is clicked. – Leb Jan 02 '17 at 15:21
1

Create a MyPicker class :)

    import static yourPackageName.MainActivity.sdf; // get static imports


    public class MyPicker extends DatePickerDialog {

    Context context;
    @Override
    public DatePicker getDatePicker() {
        return super.getDatePicker();
    }


    public MyPicker(Context context, int themeResId, OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) {
        super(context, themeResId, listener, year, monthOfYear, dayOfMonth);
        this.context = context;
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int dayOfMonth) {
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, month);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        datePicker.setText(sdf.format(myCalendar.getTime()));
        datePickerDialog.dismiss();
    }
}

now assuming your main class in MainActivity

public class MainActivity extends AppCompatActivity {


    public static Calendar myCalendar = Calendar.getInstance();


    public static String myFormat = "MM/dd/yy";
    public static SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
    public static EditText datePicker;
    public static MyPicker datePickerDialog;

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

        datePicker = (EditText) findViewById(R.id.popUpDate);
        datePicker.setText(sdf.format(myCalendar.getTime()));

        datePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datePickerDialog = new MyPicker(MainActivity.this, R.style.AppTheme_Dialog, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    }
                }, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
            }

        });
    }

}
}

or if you don't need to apply styles

public class MainActivity extends AppCompatActivity {

    public  String myFormat = "MM/dd/yy";
    public  SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
    public  EditText datePicker;
    final Calendar calendar = Calendar.getInstance();
    private DatePickerDialog datePickerDialog;

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

        datePicker = (EditText) findViewById(R.id.popUpDate);
        datePicker.setText(sdf.format(calendar.getTime()));

        datePicker.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                datePickerDialog = new DatePickerDialog(MainActivity.this, null, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.getDatePicker().init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
                    @Override
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        calendar.set(Calendar.YEAR, year);
                        calendar.set(Calendar.MONTH, monthOfYear);
                        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        datePicker.setText(sdf.format(calendar.getTime()));
                        datePickerDialog.dismiss();
                    }
                });
                datePickerDialog.show();
            }
        });
    }
}

Works like a charm!

Leb
  • 15,483
  • 10
  • 56
  • 75
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • I think it similar to my answer. and with your `MyPicker`, the Context you have define is not use, also the fucntion getDatePicker is not necessary – Linh Jan 10 '17 at 11:22
  • yes that's a class i used which extends DatePickerDialog. Removed as i can an posted it.In any way we are accessing the same method onDateChanged method which allows to identify date changes and with my first one i applied a theme – Charuක Jan 10 '17 at 11:36
  • @Charuka is it possible to set "today's" date through the `datePickerDialog` as the current date when the activity is called? It seems to always default to the last chosen one. – Leb Jan 12 '17 at 02:17
  • Simply call `myCalendar = Calendar.getInstance();` again when you go away from your datePickerActivity eg : `public void load(){ myCalendar = Calendar.getInstance(); Intent i = new Intent(MainActivity.this,SecondAct.class); startActivity(i); finish(); }` – Charuක Jan 12 '17 at 04:25
0

@Phan, Try with the below code

DatePickerDialog dialog;

datePicker.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        dialog = new DatePickerDialog(AddAccount.this, R.style.AppTheme_Dialog, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                datePicker.setText(sdf.format(myCalendar.getTime()));

if(dialog != null) {
dialog.dismiss();
}
            }
        }, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                myCalendar.get(Calendar.DAY_OF_MONTH));
dialog.show();
    }
});
ChaitanyaAtkuri
  • 1,672
  • 11
  • 15
  • the OP want to dismiss dialog immediately when we choose a date, not when we choose a day then press done (or press ok) – Linh Jan 10 '17 at 11:19