Question

I'm new in Android and I am doing a little and very easy app. I have two Editable EditText and one Button called Login. When I click the button, if the EditText's string is null, I want show a toast. Here's my code.

package com.example.prenotazione_esame;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class LoginActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Button button_crea = (Button) findViewById(R.id.Create);
        button_crea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                openCreateAccount();
            }
        });

        Button button_login = (Button) findViewById(R.id.Login);
        button_login.setOnClickListener(new View.OnClickListener() {        
            @Override
            public void onClick(View v) {
                executeLogin();

            }
        });

    }
   private void openCreateAccount(){
       Intent intent_crea = new Intent(this, CreateAccountActivity.class);
       startActivity(intent_crea);
    }
   private void executeLogin(){
       EditText login_text = (EditText) findViewById(R.id.UserName);
       EditText password_text = (EditText) findViewById(R.id.Password);
       if ((login_text.getText().toString()=="")|| (password_text.getText().toString() == "")){
           Toast toast = Toast.makeText(this, "Inserisci le credenziali di accesso", Toast.LENGTH_SHORT);
           toast.show();
       }


   }
}

where is the problem? Thank you.

Was it helpful?

Solution

Use the String's equals() method instead of the == operator for comparing strings:

Replace this line

if ((login_text.getText().toString()=="")|| (password_text.getText().toString() == ""))

with

if ((login_text.getText().toString().equals(""))|| (password_text.getText().toString().equals("") ))

In Java, one of the most common mistakes newcomers meet is using == to compare Strings. You have to remember, == compares the object references, not the content.

Check out number 7: Top 10 - New Java Developer Errors

OTHER TIPS

Compare Strings using :

login_text.getText().toString().equals("") 

You have to make String comparisons using myString.equals(compareString), not myString == compareString. In your case, you can use the isEmpty() method to see if the string is empty.

Try this for the if statement:

     if ((login_text.getText().toString().isEmpty())|| (password_text.getText().toString().isEmpty)){
           Toast toast = Toast.makeText(this, "Inserisci le credenziali di accesso", Toast.LENGTH_SHORT);
           toast.show();
     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top