dreamend dreamend - 1 year ago 51
Android Question

Intent Does Not Start in If-else

i'm a beginner on Android and Java. My problem is:

In my mainActivity class, i have a click event listener. And i'm trying to open an intent if the editText values are right (something like username,password) My code works when there's no if statement and starts the other activity but when i use that username password check, it is not running second activity. It's just waiting...

What's wrong with it?

package equalcorp.mobile.kemal;

import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.text.InputFilter.LengthFilter;

public class MainActivity extends Activity {

public EditText username;
public EditText password;
public TextView tw1;

public void onCreate(Bundle savedInstanceState) {

Button loginButton=(Button)findViewById(R.id.loginButton);

loginButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

String u_name=username.getText().toString();
String pass=password.getText().toString();

if(u_name=="kemal" && pass=="sifre"){

Intent i=new Intent(MainActivity.this,SecondActivity.class);




public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;


Answer Source

Yes I agree with the other answers here. You should not use == for comparing strings at any cost.

equals() is the method implemented for comapring strings. And why this is insisted because,

"==" compares the reference value of string object and equals() method compares content of the string object

the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory.

But, the equals method is actually meant to compare the contents of 2 objects, and not their location in memory.

So obviously You have to use something like,

if(u_name.equals("kemal") && pass.equals("sifre")){ //your code here }