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
Center Align Title in Action Bar Using Styles in Android
Android: Get Facebook Friends List
How to Use an Android Handler to Update a Textview in the UI Thread
Youtube Video Not Playing in Webview
Android - Ondatechangedlistener - How to Set This
How to Programmatically Uninstall a Package in Android
Android: Detect Softkeyboard Open
Wrong Fragment in Viewpager Receives Oncontextitemselected Call
Android Wear App Not Installing Through Handset
Using Onclick Attribute in Layout Xml Causes a Nosuchmethodexception in Android Dialogs
How to Add "Android Design Support Library" to Eclipse with Adt-Plugin
Android.Intent.Action.Screen_On Doesn't Work as a Receiver Intent Filter
Display a Part of the Webpage on the Webview Android
Android - Taking Photos and Saving Them with a Custom Name to a Custom Destination via Intent
Android: How Handle Message Error from the Server Using Volley