Live Character Count for Edittext

Android: Live character count for EditText

In this function the enabled variable is never used so the background colour and enabled states are always set.

void updateButtonState() {
boolean enabled = checkEditText(numberRoom);
goToRoom.setBackgroundColor(0xFFFFFFFF);
goToRoom.setEnabled(enabled);
}

I would replace with something like

void updateButtonState() {
boolean enabled = checkEditText(numberRoom);
if (enabled) {
goToRoom.setBackgroundColor(0xFFFFFFFF);
goToRoom.setEnabled(enabled);
} else {
//change them back to disabled state
}
}

Getting character count of EditText

Just grab the text in the EditText as a string and check its length:

int length = editText.getText().length();

Dynamic Word Count for EditText

One of the callbacks of a TextWatcher is afterTextChanged(Editable edit).

A Editable is what you get when you call getText on an EditText, which holds the current text, and therefore also knows the length of the current text.

So you can do something like this:

        editText.addTextChangedListener(new TextWatcher() {
...
@Override
public void afterTextChanged(Editable editable) {
String currentText = editable.toString();
int currentLength = currentText.length();
textView.setText("Current length: " + currentLength);
}
});

Android : create count down word field when user type in EditText

you can use addTextChangedListener like this

title.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
wordCount.setText(String.valueOf(110 - (title.getText().toString().length)));
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void afterTextChanged(Editable s) {

}
});


Related Topics



Leave a reply



Submit