5

I have an Edittext in my android application. I don't want to allow user to enter first space character..but after entering other charecter user can enter space also..I used

    <EditText
    android:id="@+id/editText1_in_row"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="text" 
    android:digits="_,qwertzuiopasdfghjklyxcvbnm,QWERTYUIOPASDFGHJKLZXCVBNM,0123456789">

but in this case user can not enter space.

I have also used Text Watcher but I need not to allow user at the time of entering text as android:digits works.

Sagar Bandamwar
  • 227
  • 2
  • 12
Vibhor Bhardwaj
  • 3,071
  • 5
  • 28
  • 49

11 Answers11

12
final EditText editText = (EditText)findViewById(R.id.editText1_in_row);


        InputFilter filter = new InputFilter() { 
            boolean canEnterSpace = false;

            public CharSequence filter(CharSequence source, int start, int end,
                    Spanned dest, int dstart, int dend) {

                if(editText.getText().toString().equals(""))
                {
                    canEnterSpace = false;
                }

                StringBuilder builder = new StringBuilder();

                for (int i = start; i < end; i++) { 
                    char currentChar = source.charAt(i);

                    if (Character.isLetterOrDigit(currentChar) || currentChar == '_') {
                        builder.append(currentChar);
                        canEnterSpace = true;
                    }

                    if(Character.isWhitespace(currentChar) && canEnterSpace) {
                        builder.append(currentChar);
                    }


                }
                return builder.toString();          
            }

        };


        editText.setFilters(new InputFilter[]{filter});

and remove this property from your EditText

android:digits="_,qwertzuiopasdfghjklyxcvbnm,QWERTYUIOPASDFGHJKLZXCVBNM,0123456789"

This code works exactly according to your needs.

Abhishek V
  • 12,488
  • 6
  • 51
  • 63
  • I think that you don't need a reference to `editText` to retrieve the text. You have all you need on `source` parameter. – JJ86 Jan 05 '21 at 15:19
6

Using InputFilter easy to handle enter first white space character ignore

First setFilters() method on editText

editText.setFilters(new InputFilter[]{ignoreFirstWhiteSpace()});

Make InputFilter

    // ignore enter First space on edittext
    public InputFilter ignoreFirstWhiteSpace() {
        return 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.isWhitespace(source.charAt(i))) {
                        if (dstart == 0)
                            return "";
                    }
                }
                return null;
            }
        };
    }

No need to write android:digits property on XML

remove this line

android:digits="_,qwertzuiopasdfghjklyxcvbnm,QWERTYUIOPASDFGHJKLZXCVBNM,0123456789"
Parth Vora
  • 1,718
  • 13
  • 9
2

Simply restrict the user to type space as others said on start only:

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            String text = createPL.getText().toString();

            //restrict space for first char
            if (text.startsWith(" ")) {
                edittext.setText(text.trim());
            }

        }
Ranjit
  • 5,130
  • 3
  • 30
  • 66
1

Use this. If the character at starting Position is a space, set textView Text To blank

editText1_in_row.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.length()>0 && s.subSequence(0, 1).toString().equalsIgnoreCase(" ")) {
                        editText1_in_row.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

                }
            });
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
1

This works for me

 android:inputType="textPersonName"
 android:digits= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_-!@#$%^*()"
Kashif Ahmed
  • 803
  • 9
  • 20
0

why cant you use editText.getText().trim(); function while using the EditText data

LMK
  • 2,882
  • 5
  • 28
  • 52
  • yes I can use but I want to to restrict user (means if user press button for the first letter then it will not work and after that it will work) – Vibhor Bhardwaj Oct 22 '13 at 08:45
0

If you want to filter input characters in your EditText, you need to use InputFilter. Here is example. //Allow only letters or digits

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

 EditText text = (EditText)findViewById(R.id.edittext1);
text.setFilters(new InputFilter[]{filter});

For details look here

Community
  • 1
  • 1
Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30
0

A slight variation on https://stackoverflow.com/users/2868352/abhishek-v answer.

public class NoInitialSpaceFilter implements InputFilter {
    @Override
    public CharSequence filter(final CharSequence source, final int start, final int end, final Spanned dest, final int dstart, final int dend) {
        if (dstart == 0) {
            for (int i = start; i < end; i++) {
                if (Character.isSpaceChar(source.charAt(i))) {
                    return "";
                }
            }
        }
        return null;
    }
}

Usage:

editText.setFilters(new InputFilter[]{new NoInitialSpaceFilter});
Community
  • 1
  • 1
Barry Irvine
  • 13,858
  • 3
  • 25
  • 36
0
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.,_-!@#$()+=><:;?"
4b0
  • 21,981
  • 30
  • 95
  • 142
0

This is filter I have used for Name validation in EditText. First letter CAPS, not space and special character. After completing the word not allow more than a single space.

public void setNameFilter() {
    InputFilter filter = new InputFilter() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            for (int i = start; i < end; i++) {
                if (dend == 0) {
                    if (Character.isSpaceChar(source.charAt(i)) ||
                            !Character.isAlphabetic(source.charAt(i))) {
                        return Constants.Delimiter.BLANK;
                    } else {
                        return String.valueOf(source.charAt(i)).toUpperCase();
                    }
                } else if (Character.isSpaceChar(source.charAt(i)) &&
                        String.valueOf(dest).endsWith(Constants.Delimiter.ONE_SPACE)) {
                    return Constants.Delimiter.BLANK;
                } else if ((!Character.isSpaceChar(source.charAt(i)) &&
                        !Character.isAlphabetic(source.charAt(i)))) {
                    return Constants.Delimiter.BLANK;
                }
            }
            return null;
        }
    };
    editText.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(Constants.Length.NAME_LENGTH)});
}
u_pendra
  • 908
  • 1
  • 10
  • 25
0

here is kotlin extension

// ignore enter First space on edittext
fun EditText.filterFirstSpace() {
val initSpaceFilter: InputFilter = object : InputFilter {
    var canEnterSpace = false
    override fun filter(
        source: CharSequence, start: Int, end: Int,
        dest: Spanned, dstart: Int, dend: Int,
    ): CharSequence {
        if (this@filterFirstSpace.text.toString() == "") {
            canEnterSpace = false
        }
        val builder = StringBuilder()
        for (i in start until end) {
            val currentChar = source[i]
            if (Character.isLetterOrDigit(currentChar) || currentChar == '_') {
                builder.append(currentChar)
                canEnterSpace = true
            }
            if (Character.isWhitespace(currentChar) && canEnterSpace) {
                builder.append(currentChar)
            }
        }
        return builder.toString()
    }
}
filters  = arrayOf(initSpaceFilter)
}
Mubashir Murtaza
  • 327
  • 2
  • 14