I was wondering how I could have an EditText, in which the user has to type in the specific format of "dd/mm/yyyy". I have already used the "datetime" inputType but this only changes the keys on the keyboard.
-
1Take a look at this question: http://stackoverflow.com/questions/16889502/how-to-mask-an-edittext-to-show-the-dd-mm-yyyy-date-format – Tomislav Jul 03 '15 at 07:49
-
This control looks like what you are looking for [https://github.com/toshikurauchi/MaskedEditText](https://github.com/toshikurauchi/MaskedEditText) – Sebastian Pakieła Jul 03 '15 at 07:47
-
http://developer.android.com/reference/java/text/SimpleDateFormat.html You could use this to format the data/time on the go. – Laurens Jul 03 '15 at 07:50
4 Answers
just add this to your Oncreate
et5.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
//Do Nothing
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
//Do Nothing
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
ss =input_Dob.getText().toString();
int o = 0;
if ((ss.charAt(2) == '/') && (ss.charAt(4) == '/')) {
Toast.makeText(Create_An_Account.this, "Format Is right", Toast.LENGTH_LONG).show();
} else {
tv5.setTextColor(Color.RED);
tv5.setText("Invalid Format");
}
ss = "";
}
});

- 177
- 10
I think that the only way that this could be done is to type a hint in this EditText
, that will be visualized like DD/MM/YYYY, so that the user can enter it in that format (hinted). I don't thin kthere's a way to force it like you want. You can enter the separate components, like a a separate field for DD, MM and YYYY and have constraints on those, then concatenate etc.

- 6,752
- 2
- 30
- 43
use this in Oncreate.
Suppose that edit text id is edittext1 .
et1=(EditText)findviewbyid(R.id.editText1)
et1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
// Add Here whatever you want to do before text changed
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
/*My below example is to check email that it must contain .com & @ */
ss ="oooo"+input_Email.getText().toString();
a = ss.length();
a--;
int o = 0;
if ((ss.charAt(a) == 'm') && (ss.charAt(a - 1) == 'o')
&& (ss.charAt(a - 2) == 'c')
&& (ss.charAt(a - 3) == '.')) {
o = 1;
while (a >= 0) {
aa = ss.charAt(a);
if (aa == '@' || aa == '.') {
j++;
}
a--;
}
}
if ((j >= 2) && (o == 1)) {
tv8.setTextColor(Color.GREEN);
tv8.setText("Correct");
Email_Check="Correct";
} else {
tv8.setTextColor(Color.RED);
tv8.setText("Invalid Email Address");
Email_Check="Invalid";
}
ss = "";
}
});

- 177
- 10
You can use following regex
"[0-9]{2}[/][0-9]{2}[/][0-9]{4}"
and validate the user input and force user to enter in dd/mm/yyyy format.
But If you want to do this dynamically means convert user input automatically as soon as he enters date into dd/mm/yyyy format) then you have to customize your edit text view and implement custom input filter where you have to write the logic.

- 611
- 1
- 5
- 13
-
This didnt work. user could put in as many characters as they wanted still – Adrian Le Roy Devezin Nov 08 '17 at 20:24