0

I have an android app where the user has to enter his name by using Input with an AlertDialog, and I am wondering if its possible to prevent the user from entering other languages except English.

enter image description here

here is the code that I use:

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

alert.setTitle("Title");
alert.setMessage("Message");

// Set an EditText view to get user input 
final EditText input = new EditText(this);
alert.setView(input);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
  Editable value = input.getText();
  // Do something with value!
  }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
    // Canceled.
  }
});

alert.show();

}
user3891365
  • 71
  • 1
  • 4
  • 13

3 Answers3

2

try with this attribute , with edittext

 android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

with java code

 editText.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"));
Sainath Patwary karnate
  • 3,165
  • 1
  • 16
  • 18
0

use this

InputFilter filter = new InputFilter() { 
    public CharSequence filter(CharSequence source, int start, int end, 
   Spanned dest, int dstart, int dend) { 
            for (int i = start; i < end; i++) { 
                    if (!Character.isLetterOrDigit(source.charAt(i))) { 
                            return ""; 
                    } 
            } 
            return null; 
    } 
  }; 

 edit.setFilters(new InputFilter[]{filter});
Meenal
  • 2,879
  • 5
  • 19
  • 43
0

In the XML file you need to put this,

android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "

also check this.

Community
  • 1
  • 1
Sabri Meviş
  • 2,231
  • 1
  • 32
  • 38