1

My aim is to print out a toast message if the user does not enter some data. I have tried an if statement, but it does not seem to work. Any suggestions.

Thanks

    Data data = new Data(data);
    FirebaseUser user = firebaseAuth.getCurrentUser();
    if(etAddNewTopic.getText().toString() == null || etAddNewTopic.getText().toString() == "") {
        Toast.makeText(getBaseContext(), "Field cannot be left blank", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this, NewActivity.class));
    } else {
        databaseReference.child(user.getUid()).setValue(data);
        databaseReference.child("Data List").setValue(data);
    }

    progressDialog.setMessage("Adding Data...");
    progressDialog.show();
    startActivity(new Intent(getApplicationContext(), DataListActivity.class));

}
Khaledonia
  • 2,054
  • 3
  • 15
  • 33
hhh
  • 23
  • 1
  • 1
  • 4

5 Answers5

1

You want:

if(etAddNewTopic.getText().toString() == null || etAddNewTopic.getText().toString().equals("")) {

The first half of your if statement is fine, but the half where you see if the string is empty is not quite right. Your current code compares object references using ==, but you want to compare object values, requiring use of the equals() method. Read more here.

Community
  • 1
  • 1
The Kraken
  • 3,158
  • 5
  • 30
  • 67
1

To throw another solution into the mix, the null and isEmpty() checks can be combined into a single statement using TextUtils.isEmpty(). So:

if(etAddNewTopic.getText().toString() == null || etAddNewTopic.getText().toString() == "") {

Becomes

if (TextUtils.isEmpty(etAddNewTopic.getText().toString())) {

I'm guessing Toasts work fine outside of that statement?

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
0

Your query is entirely Firebase-irrelevant. You are asking how to determine whether or not the the editText attribute is filled or empty.

This can be easily achieved, following the bit of code

first initializing EditText and assigning

private EditText mName

mName = (EditText) findViewById(R.id.mName);

finally, our registration function, where we determine whether Text is filled or empty

 private void registerUser() {

    final String namos = mName.getText().toString();


    if(!TextUtils.isEmpty(namos)) {

    // Text is not empty, register user with FB !

      } else {


    // Text is empty, toast to user they need mind the gap first
   Toast.maketext(YourClassName.this," Kindly mind the gap ", Toast.LENGTH_SHORT).show();

    }

result should look something like this

enter image description here

Khaledonia
  • 2,054
  • 3
  • 15
  • 33
0

Your code for checking empty string:

etAddNewTopic.getText().toString() == ""

change it to like this one:

etAddNewTopic.getText().toString().isEmpty()
Wajid
  • 2,235
  • 1
  • 19
  • 29
0
if(etAddNewTopic.getText().toString() == null || etAddNewTopic.getText().toString() == "") {

Should be as below

if(etAddNewTopic.getText().toString().trim().length() < 1) {
..........

This will also prevent to assume space as string.

kbhatta
  • 457
  • 3
  • 12