I am a new developer on Android and Java. How can I make at least 10 characters in EditText ? Also, when the user enter a value less than 10, the application send an error message on screen. How can I do these ? [ edittext > = 10 ]
1
-
edittext.getText.toString.length() >= 10 – Rajesh Mikkilineni Jul 21 '14 at 10:35
Add a comment
|
Use something like this:
EditText et = (EditText) findViewById(YOUR_EDITTEXT);
String s = et.getText().toString();
if(s.length() <= 10){
et.setError("Must exceed 10 characters!");
} else {
// ...
}
-
-
@Gooey I tried to your answer, but it has an error. "The method setError(String) is undefined for type String. " – ogulcan1912 Jul 21 '14 at 10:44
-
@ogulcan1912 the method setError should be applied to your EditText, not the string. – Gooey Jul 21 '14 at 10:46
-
It'w working.. final String emailstr = email.getText().toString();if(emailstr.length()==0) { //emailstr.requestFocus(); email.setError("FIELD CANNOT BE EMPTY"); Toast.makeText(getApplicationContext(), "Empty string", Toast.LENGTH_SHORT).show(); } – gnganapath Feb 16 '15 at 15:00
You can do that in several ways, but you can try this way:
if (myEditText.getText().length() < minLength) {
//Your message to there is no enough caracters
} else {
//Your action if it is satisfied.
}
You can set minLenght to 10, or whatever, or simply ask if value is less than 10.
I hope that you get idea from this.
You can use text watcher to check the user input and decide what to do inside
EditText editText = new EditText(this);
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) {
}
});
Yo can validate your TextView
and if it doesn't fit your requirements, use myTextView.setError(String)
to show an error.
If not you will have to implement myTextView.addTextChangedListener(...)
and do things manually.
Hope it helps.