1

How can we limit the EditText length to certain number of word count (100 words).

Even though copy a large text from some where,while pasting it should accept only 100 words. I know we can limit it by character count (maxLength)

I tried with below code, but it doesn't limit it by exactly 100 words and it is allowing copy-paste large text.

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

                int wordsLength = countWords(charSequence.toString());// words.length;
                // count == 0 means a new word is going to start
                if (count == 0 && wordsLength >= MAX_WORD_LIMIT) {
                    int charLength = mDescription.getText().length();
                    setCharLimit(mDescription, charLength > 0 ? charLength - 2 : charLength);
                } else {
                    removeFilter(mDescription);
                }

and

private InputFilter filter;

    private void setCharLimit(EditText et, int max) {
        filter = new InputFilter.LengthFilter(max);
        et.setFilters(new InputFilter[]{filter});
    }

    private void removeFilter(EditText et) {
        if (filter != null) {
            et.setFilters(new InputFilter[0]);
            filter = null;
        }
    }
Ram Prakash Bhat
  • 1,308
  • 12
  • 21

7 Answers7

1

You can do this by using:

android:maxLength="50"

in your EditText.

Rahul Sharma
  • 12,515
  • 6
  • 21
  • 30
1

Hope its work for you

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

        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
           // Check your edittext length here
           if (edittext.getText().toString().length() > 100)
              edittext.setText(edittext.getText().toString().substring(0, 100);
        }
    });
shahid17june
  • 1,441
  • 1
  • 10
  • 15
1
    edittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

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

            //To fix word count
            String yourText= editText.getText().toString().replace(String.valueOf((char) 160), " ");
            if (yourText.split("\\s+").length > MAX_WORD_LIMIT ) {

                int space = 0;
                int length = 0;
                for (int i = 0; i < yourText.length(); i++) {
                    if (yourText.charAt(i) == ' ') {
                        space++;
                        if (space >= MAX_WORD_LIMIT) {
                            length = i;
                            break;
                        }

                    }
                }
                if (length > 1) {
                    editText.getText().delete(length, yourSelf.length()); // deleting last space
                    setCharLimit(editText, length - 1); //or limit edit text
                }
            } else {
                removeFilter(editText);
            }


        }

            @Override
            public void afterTextChanged(Editable s) {

        });
          private InputFilter filter;

          private void setCharLimit(EditText et, int max) {
            filter = new InputFilter.LengthFilter(max);
            et.setFilters(new InputFilter[] { filter });
            }

          private void removeFilter(EditText et) {
            if (filter != null) {
              et.setFilters(new InputFilter[0]);
             filter = null;
             }
           }

here 160 is the non breaking space

Ram Prakash Bhat
  • 1,308
  • 12
  • 21
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
1

Kotlin Language -- To check maximum words need to check total space

--> Step-1

var filter: InputFilter?=null

--> Step-2


edt.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {

                var count : Int = 0

                if(s.toString().length > 0)

                    for(i in 0..s.toString().length-1){

                        if(s.toString()[i].toString() == " "){
                            count++
                        }

                        if(i > 1 && s.toString()[i-1].toString() == " " && s.toString()[i].toString() == " "){
                            count--
                        }

                    }

                if (count >= MAX_COUNTS_WORDS){
                    filter = InputFilter.LengthFilter(edt.text.toString().length)
                    edt.filters = arrayOf<InputFilter>(filter ?: return)
                }
                else if (filter != null) {
                    edt.filters = arrayOfNulls(0)
                    filter = null

                }


            }

            override fun beforeTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

        })
Avinash Kahar
  • 129
  • 1
  • 5
0

If you want to find word count then you can use java logic.

String text = "More than 100 word sentence";
String[] txtArr = text.split(" ");

Now Pick only first 100 index words.

This is just a hack. May be there are more optimal ways out there.

Ashish M
  • 763
  • 6
  • 13
0

You can use split with index of functions. Sorry for so long string, hope you will get the idea. Just extract some variables.

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

            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {

               if (edittext.getText().toString().split(" ").size() > 100)
                  edittext.setText(edittext.getText().toString().substring(0, edittext.getText().toString().indexOf(edittext.getText().toString().split(" ").get(100))));
            }
        });
A. Shevchuk
  • 2,109
  • 14
  • 23
0

use android:maxLength property of edittext to solve your problem.