3

I have an EditText wrap with TextInputLayout - that will get an organization's mission/vision.

The TextInputLayout will notify how many words left, it will split the string if encountered a space.

The problem was it doesn't count words, instead it count characters.

Here's my sample code:

appCompatEditTextEventThirdContents.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) {
        final int maxWords = 500;
        String[] words = s.toString().split(" ");
        int currentWords = words.length;
        try{
            if (currentWords < maxWords) {
                appCompatEditTextEventThirdContents.setHint("Third Paragraph " + (maxWords - currentWords) + "left");
            }
            else if (currentWords >= maxWords ){
                appCompatEditTextEventThirdContents.setHint("Exceeded 500 words.");

            }
        }catch (NumberFormatException e){
            e.printStackTrace();
        }
    }
});

Here's my layout view.

<!--THIRD PARAGRAPH-->
<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textInputThirdEventParag"
    android:hint="@string/paragThird"
    android:layout_below="@+id/textInputSecondEventParag"
    android:layout_centerHorizontal="true">

    <android.support.v7.widget.AppCompatEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editTextPG3"
        android:scrollbars="vertical|horizontal"
        android:minLines="5"
        android:maxLines="10"
        android:maxLength="500"
        android:singleLine="false"/>

</android.support.design.widget.TextInputLayout>

I base splitting strings here.

Community
  • 1
  • 1
RoCkDevstack
  • 3,517
  • 7
  • 34
  • 56

1 Answers1

1

The attribute maxLength limits the character in the EditText. So in your case you need to remove the attribute. You need to remove the maxLines attribute too.

<android.support.v7.widget.AppCompatEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editTextPG3"
    android:scrollbars="vertical|horizontal"
    android:minLines="5"
    android:singleLine="false"/>

So now, you need an InputFilter along with the TextWatcher to set the maximum word limit.

Prepare your InputFilter like this.

private InputFilter mInputFilter;

// Helping functions to dynamically add or remove filters from your EditText
private void forceFilter(EditText mEditText, int charCount) {
    mInputFilter = new InputFilter.LengthFilter(charCount);
    mEditText.setFilters(new InputFilter[] { mInputFilter });
}

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

Now declare a function to get the word count

private int getWordsCount(String input) {
    String trim = input.trim();
    if (trim.isEmpty())
        return 0;
    return trim.split("\\s+").length; // Separate string by spaces
}

Now you have to add a TextWatcher to dynamically add or remove your InputFilter around your EditText.

private final int MAX_WORD_LIMIT = 500;
private EditText mEditText = (EditText) findViewById(R.id.your_edit_text);

appCompatEditTextEventThirdContents.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        int totalWordCount = getWordCount(s.toString());

        // totalWordCount = 0 means a new word is going to start
        if (count == 0 && totalWordCount >= MAX_WORD_LIMIT) {
            forceFilter(mEditText, mEditText.getText().length());
            appCompatEditTextEventThirdContents.setHint("Exceeded 500 words.");
        } else {
            removeFilter(mEditText);
            appCompatEditTextEventThirdContents.setHint("Third Paragraph " + (maxWords - currentWords) + "left");
        }
    }

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

    @Override
    public void afterTextChanged(Editable s) {}
});

You can not handle maximum word count in the paste event with this. If you want to filter the paste events too you might take a look here.

Community
  • 1
  • 1
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98