I'm new to Android Studio, I want to display a toast when a button is pressed if one or both of two editText fields is empty? I've tried for 5 hours; about to die.
Asked
Active
Viewed 1,111 times
-2
-
3Possible duplicate of [Check if EditText is empty.](http://stackoverflow.com/questions/6290531/check-if-edittext-is-empty) – Amit Singh Jun 24 '16 at 06:54
-
please post your java file . – Sagar Nayak Jun 24 '16 at 07:28
4 Answers
1
First off, create a new method in your class if you haven't already:
public void checkIfEmpty(View v){
if(editText.getText().toString().isEmpty() & editText2.getText().toString().isEmpty()){
Toast.makeText(this, "EditText is empty", Toast.LENGTH_SHORT).show();
}
}
And in your xml, be sure to set the onClick to that method. If you're new to java and you didn't understand tell me.

Ali Bdeir
- 4,151
- 10
- 57
- 117
-
Hey it worked but I'm trying to open a new activity if it is false. The code I have here opens the new activity regardless. – Patrick J. Jun 24 '16 at 20:04
-
public void log_in_sends_2(View view) { if (EditUsername.getText().toString().isEmpty() & EditPassword.getText().toString().isEmpty()) { Toast.makeText(this, "EditText is empty", Toast.LENGTH_SHORT).show(); { String LogIn_Button; LogIn_Button = ((Button) view).getText().toString(); if (LogIn_Button.equals("Log In")) { Intent intent = new Intent(this, LogIn.class); startActivity(intent); } } } } } – Patrick J. Jun 24 '16 at 20:05
-
0
You will have to set an onClickListener
to your button. Then, inside it, add this code:
if(mTextView.getText().toString().equals("")){
makeToast(mContext, "TextView is empty!", Toast.LENGTH_SHORT).show();
}
A few notes about this:
- mTextView and mContext are initialized in your Java class outside the listener, so in order to use inside it they have to be declared
final
- When you call
makeToast
always remember to callshow()
at the end or it will never be displayed

Aurasphere
- 3,841
- 12
- 44
- 71
-
-
Also I tries to use this code to set an onClickListener for the button by I'm getting errors, Button LogInButton = (Button) findViewById(R.I'd.button_logIn_Main); LogInButton.setonClickListener(new Button.on.ClickListener() { public void mybuttonid = (View view) { \\other code } – Patrick J. Jun 24 '16 at 07:19
-
By final I mean like this: final Context mContext = getActivity (); final TextView mTextView = (TextView) rootView.findViewById (R.id.myView); If you have other errors, please post them. – Aurasphere Jun 24 '16 at 07:24
0
Courtesy Answer by cvaldemar here
EditText text1 = (EditText) findViewById(R.id.editUsername);
EditText text2 = (EditText) findViewById(R.id.userEmail);
name = text1.getText().toString();
email = text2.getText().toString();
if (name.matches("") || email.matches(""))
{
Toast.makeText(this, "You did not enter a username",Toast.LENGTH_SHORT).show();
return;
}

Community
- 1
- 1

Amit Singh
- 2,698
- 21
- 49
-
You need to create the id by which you are fetching the EditTextFields in xml file – Amit Singh Jun 25 '16 at 05:19
-
After that you will be able to fetch the fields, the above id's are just for demos. This is how you can fetch the EditText and apply the condition and display the Toast. – Amit Singh Jun 25 '16 at 05:20
0
Take values from editText on button click and compare them with string .
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
strEdt1 = editText1.getText().toString();
strEdt2 = editText2.getText().toString();;
if (editText1.isEmpty() && editText2.isEmpty()) {
// Toast for editText1
editText1.requestFocus();
}
else if (editText2.isEmpty()) {
// Toast for editText2
editText2.requestFocus();
}
else if (editText1.isEmpty()) {
// Toast for editText2
editText1.requestFocus();
}
}
});

Gevaria Purva
- 552
- 5
- 15