KiKo KiKo - 7 days ago 4
Android Question

AlertDialog with EditText with InputType email - Android

I'm creating alert dialog with one EditText. I want that EditText to have input type email. This is my code:

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setMessage("Enter your email");

final EditText email = new EditText(this);
email.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
email.setHint("Email...");

alert.setView(email);

alert.setPositiveButton("Ok", null);

alert.setNegativeButton("Cancel", null);

alert.show();


I also set Hint to that EditText and it's work, but input type does't work.....Any suggestion?

Answer

You have to add below code to make it work :

email.setInputType(InputType.TYPE_CLASS_TEXT 
          | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

See InputType. TYPE_TEXT_VARIATION_EMAIL_ADDRESS is just a variation basically that you add to the TYPE_CLASS_TEXT flag. It somehow makes sense.

Comments