1

Description

I am developing one app in which I have registration page. Inside registration page, I am doing registration by getting user's full name and mobile number.

Problem

While getting user's full name in edit text sometimes the user is pressing space bar before type his/her name.

I need you disable space-bar key before typing any text white user start typing his name I want to enable space-bar key. So that user can enter space between his/her Middle name and Last name.

What I have tried?

Answer

I am using text watcher in edit text.

user_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                inputLayoutname.setError(null);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                System.out.println(TAG+" s :"+s+ " start :"+start+" Before : "+before+" Count: "+count);
                String str = s.toString();

                if(str.length() > 0 && str.contains(" "))
                {
                    user_name.setError("Space is not allowed");
                    user_name.setText("");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {    

                if (user_name.getText().length() > 0)
                    inputLayoutname.setError(null);
            }
        });

What problem coming when I am implementing this code?

While user pressing firstly it automatically removing space suddenly. But when user trying to enter space between full name, it's again removing all text and showing empty edit text.

screen 1 while entering only space enter image description here

Here I am entering my Name and want to enter last name or middle name of space enter image description here

Here when I am entering space after my first name enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • user_name.setText(str.trim()); or user_name.setText(str.replaceAll(" ",""); instead of user_name.setText(""); – Jyoti JK Jan 24 '18 at 11:14
  • @Joe I am trying like this now its not removing the written text but space also not coming .now courser is going to start. if(str.length() > 0 && str.contains(" ")) { user_name.setError("Space is not allowed"); //user_name.setText(""); user_name.setText(str.replaceAll(" ","")); } – ABHAY PRATAP SINGH Jan 24 '18 at 11:38

4 Answers4

3

Use trim() method to remove the extra space

user_name.getText().toString().trim().

And Remove

if(str.length() > 0 && str.contains(" "))
                {
                    user_name.setError("Space is not allowed");
                    user_name.setText("");
                }

from your onTextChanged

To prevent user from entering space add this on your onTextChanged

if(str.equals(" "))
                {
                    user_name.setError("Leading Space is not allowed");
                    user_name.setText("");
                }
Community
  • 1
  • 1
Dipali Shah
  • 3,742
  • 32
  • 47
2

First, try to understand what he is trying to do

He is trying to prevent user typing leading spaces and not after entering text.

He doesn't want to trim username

Update your code from

if(str.length() > 0 && str.contains(" "))
                {
                    user_name.setError("Space is not allowed");
                    user_name.setText("");
                }

to

if(str.equals(" "))
                {
                    user_name.setError("Leading Space is not allowed");
                    user_name.setText("");
                }

It will prevent user typing any space before name

Naveen Dew
  • 1,295
  • 11
  • 19
1

use this trim() method to remove the space like this..

String str = s.toString().trim();
Vikas singh
  • 3,461
  • 3
  • 14
  • 28
  • @ABHAY PRATAP SINGH there is no need to disable and enable space bar.you just use `trim()` method it will remove your prefix and suffix spaces – Vikas singh Jan 24 '18 at 11:30
  • I am trying like this now its not removing the written text but space also not coming .now courser is going to start. if(str.length() > 0 && str.contains(" ")) { user_name.setError("Space is not allowed"); //user_name.setText(""); user_name.setText(str.replaceAll(" ","")); } – ABHAY PRATAP SINGH Jan 24 '18 at 11:40
0

Try this, check if start == 0, it will not allow user to add spaces before name

@Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(start == 0 && s.toString().contains(" ")){
                user_name.setText("");
            }
        }