Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict TextView to allow only alpha-numeric characters in Android

I have a TextView in my app that i want a user to be able to only enter alpha-numeric characters in. How can this be done? Thanks!

like image 985
android-developer Avatar asked May 21 '11 16:05

android-developer


People also ask

How can add only numbers in EditText in android?

You can use android:inputType="number" in the XML file. You can specify other values such as numberDecimal as well. Also, you might additionally want to use android:singleLine="true" for a single line Edittext .

What is Imeoption android?

android:imeOptions="actionSend" /> You can then listen for presses on the action button by defining a TextView.OnEditorActionListener for the EditText element. In your listener, respond to the appropriate IME action ID defined in the EditorInfo class, such as IME_ACTION_SEND . For example: findViewById<EditText>(R.

What is editable TextView in android?

Android App Development for Beginners A EditText is an overlay over TextView that configures itself to be editable. It is the predefined subclass of TextView that includes rich editing capabilities.


2 Answers

In the XML, put this:

android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "
like image 130
strange quark Avatar answered Dec 19 '22 20:12

strange quark


Here is a better solution......... https://groups.google.com/forum/?fromgroups=#!topic/android-developers/hS9Xj3zFwZA

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});
like image 38
Ach J Avatar answered Dec 19 '22 20:12

Ach J