Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android disable space only for Edittext

In my android application I need to disABLE Spacebar only. But I didn't find a solution for this problem. I need to disable space bar and when user enter space should not work, special characters, letters, digits and all other should work. What I tried is,

etPass.addTextChangedListener(new TextWatcher() {              @Override             public void onTextChanged(CharSequence s, int start, int before, int count) {                 // TODO Auto-generated method stub                 String str = s.toString();                 if(str.length() > 0 && str.contains(" "))                 {                     etPass.setError("Space is not allowed");                     etPass.setText("");                 }             }              @Override             public void beforeTextChanged(CharSequence s, int start, int count,                                           int after) {                 // TODO Auto-generated method stub              }              @Override             public void afterTextChanged(Editable s) {                 // TODO Auto-generated method stub              }         }); 

But the problem here is once space comes whole text is deleting. I removed

etPass.setText(""); 

this line, so at that time error message is showing, but at that time user can still able to type space. But what I need is user shouldn't able to type the space.

like image 829
roshanpeter Avatar asked Nov 30 '15 07:11

roshanpeter


1 Answers

This solution worked for me :

android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890" android:inputType="textFilter" 

Add it into edit text in the XML file

like image 115
SAndroidD Avatar answered Oct 19 '22 23:10

SAndroidD