How To Check Whether A Value Is Entered In Editexts Before Submitting?
I have around 5 edittexts.When i click submit button,it has to check whether all fields are entered or not.Along with it if any value is not entered,the focus has to go to that des
Solution 1:
Android provides a setError()
method for this:
if(edttxtDescription.getText().toString().trim().equals(""))
{
edttxtDescription.setError("Please provide description");
}
Define a method to check whether your EditText
s have valid data:
privatebooleanvalidateEditTexts()
{
boolean valid = true;
if(edttxtDescription.getText().toString().trim().equals(""))
{
edttxtDescription.setError("Please provide description");
valid = false;
}
// Similarly check all your EditTexts here and set the value of valid
......
......
return valid;
}
To validate all your EditTexts
, call validateEditTexts()
which will return true
or false
accordingly.
btnsubmit.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
if(validateEditTexts()){
Send_reportclaim_Async reportsync=newSend_reportclaim_Async();
reportsync.execute();
}
}
});
Try this. This will work.
Solution 2:
Check this:
for(EditText edit : editTextList){
if(TextUtils.isEmpty(edit.getText()){
// EditText is empty
}
}
Solution 3:
Maintain array of EditText references: Like
EditText[] allEts = { caseno, dateloss, policy_rep, reg_book, Dri_lic };
Write the below code in onClick of submit button:
for (EditText editText : allEts) {
Stringtext= editText.getText().toString();
if (text.length() == 0) {
editText.setError("enter this field");
editText.requestFocus();
break;
}
}
And, implement addTextChangedListener for all edittexts to clear the error after entering the text.
caseno.addTextChangedListener(newTextWatcher() {
@OverridepublicvoidonTextChanged(CharSequence s, int start, int before,
int count) {
Editabletext= caseno.getText();
if (caseno.getError() != null && text != null
&& text.length() > 0) {
caseno.setError(null);
}
}
@OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@OverridepublicvoidafterTextChanged(Editable s) {
}
});
Solution 4:
Use this on your button click
if(!textView1.toString().isEmpty() && !textView2.toString().isEmpty() && ...)
{
............
}
Solution 5:
1) create this method
publicstaticbooleanisNullOrEmpty(String input) {
return input == null || input.isEmpty();
}
2) send data to it for validation
booleananswer= isNullOrEmpty(editText.gettext().toString());
Post a Comment for "How To Check Whether A Value Is Entered In Editexts Before Submitting?"