0

I want to create a textedit field where the user enters a date only (no time). The date will get stored in MY SQL. Whats the best way to do this with the least amount of validation? Is there like a built in textfield for dates that keeps it in the proper format?

I have this:

public static void AddEditTextDate(Context context, LinearLayout linearlayout, String text, int id) {
    EditText edittext = new EditText(context);
    edittext.setInputType(InputType.TYPE_DATETIME_VARIATION_DATE);
    edittext.setText(text);
    edittext.setId(id);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
    edittext.setLayoutParams(params);
    linearlayout.addView(edittext);
}

But when I try to type in it, it looks like a normal keyboard. I would expect it to go into the number pad by default or something...

EDIT: It needs to work with android 2.1+ (i.e. v7)

Does anyone know?

Thanks

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
sneaky
  • 2,141
  • 9
  • 31
  • 38
  • I would really recommend you to try to define it in some xml file and load it when necessary. And test some other input types like numbers/ phone etc. – Mateusz Zając Jul 27 '13 at 21:40
  • I need to insert them dynamically because the user can change their data. – sneaky Jul 27 '13 at 21:41
  • 1
    Use `InputType.TYPE_CLASS_DATETIME` instead of `InputType.TYPE_DATETIME_VARIATION_DATE` to show the numbers-only keyboard. You will of course need to validate the date and format after user input. You can use `regex` for that. – Vikram Jul 28 '13 at 01:19

1 Answers1

2

You said Whats the best way to do this with the least amount of validation? Is there like a built in textfield for dates that keeps it in the proper format?

There is one way which comes to my mind, using which you might not need to check any validation of the date format that user entered. You can call a DatePickerDialog on click of your EditText box. Then user can choose the date using that. After user has chosen the date you can update your EditText with the chosen date. This way you lesses the effort of validation of the date format entered and user can easily and intuitively choose the date. You might so something like:

Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener date = 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);
        updateLabel();
    }

};
//When the editText is clicked then popup the DatePicker dialog to enable user choose the date       
edittext.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {
         // TODO Auto-generated method stub
         new DatePickerDialog(new_split.this, date, myCalendar
                 .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                 myCalendar.get(Calendar.DAY_OF_MONTH)).show();
     }
 });
// Call this whn the user has chosen the date and set the Date in the EditText in format that you wish
 private void updateLabel() {

    String myFormat = "MM/dd/yyyy"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);   
    edittext.setText(sdf.format(myCalendar.getTime()));
 }

Source: This answer on Datepicker: How to popup datepicker when click on edittext question. Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • The DatePicker class itself is since API level 1. You could just create a Dialog with AlertDialog.Builder and set its content view to be a DatePicker instance. (Alternatively, using ActionBarSherlock you could use SherlockDialogFragment and place the DatePicker in there) – Karakuri Jul 27 '13 at 22:54
  • @Karakuri is right. You can use this with android 2.1 . You can also go for ActionBarSherlock as he rightly mentioned. – Shobhit Puri Jul 27 '13 at 23:22