Reputation: 1581
I'm having trouble with validation input from EditText. Whenever I click on SaveData button, the dialop popup no matter what it is valid or not. Here is the sample what I wrote:
Validation class:
package android.week04Lab03;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.webkit.URLUtil;
import android.widget.EditText;
public class Validation {
public boolean isEmpty(EditText txt){
if(txt.getText().toString().equals("")){
return false;
} else
return true;
}
public boolean isURL (EditText txt){
if (!URLUtil.isValidUrl(txt.getText().toString())){
return false;
}
return true;
}
public boolean isEmail (EditText txt){
String isEmail = "^([^@\\s]+)@((?:[-a-z0-9]+\\.)+[a-z]{2,})$";
Pattern isEmailPattern = Pattern.compile(isEmail);
Matcher mailMatcher = isEmailPattern.matcher(txt.getText().toString());
if (!mailMatcher.find()) {
return false;
}
return true;
}
}
Here is the activity class
public class MetaData extends Activity {
Validation validator;
ImageView angel;
EditText name, loc, keyword, date, mail;
ToggleButton share;
RatingBar rating;
Button save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meta_data);
name = (EditText) findViewById(R.id.name);
..........................
public void saveData(View v) {
Validation validator = new Validation();
if (!validator.isEmpty(name)) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Input Validation Warning");
alert.setIcon(R.drawable.ic_launcher);
alert.setMessage("Error found, please enter your information correctly!");
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alert.show();
}
}
Upvotes: 0
Views: 347
Reputation: 1332
EditText e1;
e1=(EditText)findViewById(R.id.et1);
int et1=e1.getText().toString().trim().length();
public boolean isEmpty(EditText txt){
if(et1>0)
{
return false;
} else
return true;
}
try This..May Be Its Helpfull For You.
Upvotes: 0
Reputation: 30855
Just use equals() instead of == for check string is empty or not
txt.getText().toString().equals("")
instead of txt.getText().toString() == ""
== compare two object not its value
Upvotes: 2
Reputation: 7089
Instead of using if(txt.getText().toString() == "")
to compare the string, could you try using if(txt.getText().toString().equals(""))
instead?
Apart from your question, I found there might be a little bit mess in your logic:
First, you did this String comparison
public boolean isEmpty(EditText txt){
if(txt.getText().toString() == ""){
return false;
} else
return true;
}
Which you were trying to do, is to return false
on your isEmpty
method if the string in the EditText is empty, or return true
otherwise.
Then, you did this if (!validator.isEmpty(name))
in your saveDate()
method, which means, according to your code:
if (validator.isEmpty(name) == false) // isEmpty returned false, means getText got an empty String
Why not make isEmpty
to return true
if the EditText is empty? That'll make you easier to understand the code later.
Upvotes: 1
Reputation: 20155
That is not the way to check whether string is empty or not. Because it will fail if you have one or more spaces in your Edittext.
So check length like this
if(txt.getText().toString().trim().length() == 0){
return false;
} else
return true;
And also you should compare strings with the equals
method.
When you give something in between "" to compare, == will also be treated as equals. So no problem in that case.
Upvotes: 2