Android check spaces in EditText

how to restrict space in edittext android
how to remove space in edittext in android

I have a problem regarding Edit Text in Android.

I have a field named Username :

I want that whenever someone writes a username with a space e.g "Gaurav Arora". Then it should raise a toast or error on the login button press.

I did in this way - I simply stopped the effect of space bar with the help of a text watcher as-

public static TextWatcher getNameTextWatcher(final TextView agrTextView) {
        TextWatcher mTextWatcherName = new TextWatcher() {
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {

            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {

            public void afterTextChanged(Editable arg0) {

                String result = agrTextView.getText().toString().replaceAll(" ", "");
                if (!agrTextView.getText().toString().equals(result)) {


But this has not solved my purpose. I just want to implement whenever the person uses a username with space it should accept the space but on the press of login button it should raise a toast


Just check if your edit text contains blank space or not.

Use the following code in the onClick() event of the Button.

 if (agrTextView.getText().toString().contains(" ")) {
     agrTextView.setError("No Spaces Allowed");
     Toast.makeText(MyActivity.this, "No Spaces Allowed", 5000).show();

Android EditText Space Validation, final EditText editText = (EditText)findViewById(; InputFilter filter = new InputFilter() { boolean canEnterSpace = false; public  Examples of Android EditText with Spaces and Limited Characters. Time:2019-8-20. Preface. When using input boxes, products often have some requirements, such as

Try this..

     btnLogin.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String result = agrTextView.getText().toString();
             if(result.contains ("\\s"))
               Toast.makeText(getApplicationContext(), "Space ", Toast.LENGTH_LONG).show();

Android check spaces in EditText, Just check if your edit text contains blank space or not. Use the following code in the onClick() event of the Button. if (agrTextView.getText().toString().contains("  private boolean isEmpty(EditText myeditText) { return myeditText.getText().toString().trim().length() == 0; } If function return false means edittext is not empty and return true means edittext is empty. For more info see this best link Check if EditText is empty.

Simply do this:

TextView.getText().toString().contains(" ");

Check blank space when clicking in a EditText, I have a customized EditText class, whats is happening is that there is a validation already for the field, checking it's length and doing trim. But, the app is​  How to find that EditText is full empty or contain any values inside it programmatically on button click. Empty data insertion is one of the most big problems in online registration word because sometimes application user just to check our app is working properly or not they trying to send empty data to server but after putting empty condition on EditText app user is compulsory bound to enter

 String question = txtNote.getText().toString();
 if(question.trim().length() > 0){
    Log.i("LOG", "Has value");
    Log.i("LOG", "don't has value");

How to remove white spaces for textview in Android?, This example demonstrates How to remove white spaces for textview in Android. MainActivity" android:orientation = "vertical"> <EditText android:id Select your mobile device as an option and then check your mobile  I have an EditText in android for users to input their AGE. It is set an inputType=phone. I would like to know if there is a way to check if this EditText is null.. I've already looked at this question: Check if EditText is empty. but it does not address the case where inputType=phone.

use trim() method to remove all spaces and then check for empty

if ( !edtText.getText().toString().trim().isEmpty()) {
  //the EditText is not Empty

Examples of Android EditText with Spaces and Limited Characters , Examples of Android EditText with Spaces and Limited Characters Monitor private TextChangeListener listener; // Additional Characters  144. For validating EditText use EditText#setError method for show error and for checking empty or null value use inbuilt android class TextUtils.isEmpty (strVar) which return true if strVar is null or zero length. EditText etUserName = (EditText) findViewById(; String strUserName = etUserName.getText().toString(); if(TextUtils.isEmpty(strUserName)) { etUserName.setError("Your message"); return; }

EditText auto add space when input phone number, Unicode: To use white space in XML as string use \u0020 XML won't take Remove fragment with clear back stack · Android soft keyboard covers EditText field: Overscroll to Soft keyboard Check android application is in foreground or not? This example demonstrates how do I check if android editText is empty. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Add blank space character into a string name in , When you define an edit text widget, you must specify the R.styleable.​TextView_inputType attribute. For example, for plain text input set inputType to "​text":. Overview. The EditText is the standard text entry widget in Android apps. If the user needs to enter text into an app, this is the primary way for them to do that. There are many important properties that can be set to customize the behavior of an EditText.

EditText, For space character the unicode value is '\u0020'. This method checks for this unicode value before and after the string and if it exists then eliminates the spaces(  EditText editText = (EditText)findViewById(; editText.setText("Username");//set the text in edit text. 4. hint: hint is an attribute used to set the hint i.e. what you want user to enter in this edit text. Whenever user start to type in edit text the hint will automatically disappear.

  • Why don't you check if there is space in the text on the button click instead?
  • Yes i want to check on the login button that if edittext has any spaces. How can I check?
  • if(string.indexOf(" ") != -1)Toast(...)
  • u can try with string.contains(" ") its Boolean method
  • read the question carefully before posting an irrelative answer. Also in case you haven't checked out the base steps for a helpful post , please see : here