Skip to content Skip to sidebar Skip to footer

Android Check Spaces In Edittext

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 rais

Solution 1:

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();
 }

Solution 2:

Try this..

     btnLogin.setOnClickListener(newOnClickListener() {

        @OverridepublicvoidonClick(View v) {
            // TODO Auto-generated method stubString result = agrTextView.getText().toString();
             if(result.contains ("\\s"))
               Toast.makeText(getApplicationContext(), "Space ", Toast.LENGTH_LONG).show();
        }
    });

Solution 3:

Simply do this:

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

Solution 4:

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

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

Solution 5:

Also you can block entering space bar in username, or can give toast or snack on entering space in username .

Can refer this link might be helpful for you Block entering space in edittext.

Let me know if any concern.

Post a Comment for "Android Check Spaces In Edittext"