
Here is my TextInputEditText inside TextInputLayout. The hint you can see is set on TextInputLayout.
The user must write 255 characters otherwise the post is not valid.
I want the digit 255 to decrease as the user types and when reached to 0, the hint should be blank.
Here is my code :
private int charactersLeft = 256; is declared outside onCreate method.
And this code is inside onCreate method :
final TextInputLayout postTextInputBase = findViewById(R.id.post_text_input_base);
final TextInputEditText postTextInput = findViewById(R.id.post_text_input);
postTextInput.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) {
        if (charactersLeft == 0) {
            postTextInputBase.setHint(null);
        } else {
            postTextInputBase.setHint((charactersLeft - 1) + " characters left");
        }
    }
    @Override
    public void afterTextChanged(Editable s) {
    }
});
I also tried to set charactersLeft to characterLeft -= 1 but, the java won't allow me to do that.
Then, how do I do this ?
Use  app:counterEnabled="true"
Also use app:counterMaxLength="250"
Try this
<android.support.design.widget.TextInputLayout
    android:id="@+id/post_text_input_base"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:counterEnabled="true"
    app:counterMaxLength="250">
    <EditText
        android:id="@+id/post_text_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="UserName" />
</android.support.design.widget.TextInputLayout>
OUTPUT

public class RecyclerViewActivity extends AppCompatActivity {
    TextInputLayout postTextInputBase;
    EditText postTextInputEditText;
    int textLength = 9;
    int len = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        postTextInputBase = findViewById(R.id.post_text_input_base);
        postTextInputEditText = findViewById(R.id.post_text_input);
        postTextInputBase.setHint(textLength + " characters left");
        postTextInputEditText.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) {
                // first check your EditText is empty or not
                if (!TextUtils.isEmpty(postTextInputEditText.getText().toString())) {
                    int len = postTextInputEditText.getText().toString().length();
                    // than check that editText text length is less than max length or not
                    // for test case i have set max length 10
                    if (len < 9) {
                        // if length of text is less than max length than use minus one from max length and set error in your text input layout
                        textLength--;
                    } else {
                        textLength++;
                        
                    }
                    
                    postTextInputBase.setHint(textLength + " characters left");
                } else {
                    textLength = 9;
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
              if(postTextInputEditText.getText().toString().isEmpty()){
                  textLength = 9;
                  postTextInputBase.setHint(textLength + " characters left");
              }
            }
        });
    }
}
OUTPUT

postTextInputBase.setHint(textLength + " characters left");
OUTPUT

NOTE Don't forgot to set
android:maxLength="10"in edittext
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With