Reputation: 20177
I'm trying to check the value of the EditText inside my AlertDialog.Builder so that I can prevent the user from entering nothing in the EditText.
For some reason when I use the code below it never gets into the if (mEditText.getText() == null || test == "")
statement, it always executes the else statement instead.
Any ideas? Thanks folks.
@Override
protected Dialog onCreateDialog(int id) {
LayoutInflater factory = LayoutInflater.from(mContext);
final View textEntryView = factory.inflate(R.layout.file_save_dialog, null);
mEditText = (EditText) textEntryView.findViewById(R.id.file_name_edit);
final Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
return new AlertDialog.Builder(mContext)
.setIcon(android.R.drawable.ic_menu_save)
.setTitle(R.string.file_name_title)
.setView(textEntryView)
.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String test = mEditText.getText().toString();
//This if never works...it always hits the else statement
if (mEditText.getText() == null || test == "") {
mEditText.startAnimation(shake);
}
else {
finish();
}
}
})
.setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
})
.create();
}
Upvotes: 1
Views: 681
Reputation: 202
If you want compare two String values, use method equals()
.
For example:
stringOne.equals(stringTwo);
Upvotes: 1
Reputation: 11541
Do it in the Android way:
if (TextUtils.isEmpty(mEditText.getText()) {
mEditText.startAnimation(shake);
} else {
finish();
}
Upvotes: 2
Reputation: 33983
put it like this
if (test == null || test.length() <= 0) {
mEditText.startAnimation(shake);
}
else {
finish();
}
Upvotes: 1