abc123 abc123 - 20 days ago 6
Android Question

EditText to enter 3 alphas and 4 digits - change input type dynamically

I need to set one of my EditText to enter three alpha value and four digits Ex:- ABC1234. So I need to change automatically input type from Text to phone when user typed three alphas. I came up with following code and but its not change to phone input type and text type is remains for any text length.

editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String text = editText.getText().toString();
if(text.length()==1 || text.length()==2 || text.isEmpty()){
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
System.out.println("Length Text type : " + text.length());

}else{
editText.setRawInputType(InputType.TYPE_CLASS_PHONE);

System.out.println("Length phone type : " + text.length());

}
}


What I have done wrong.

Answer

setRawInputType() is usually used when you initialize the view, in a constructor of a custom view or in onCreate() method of an activity.

In your case you have to call setInputType() change the mode of the soft keyboard.

Comments