0

Here I am creating a simple Droid LoginApp, which simply tells login successfull or not. But the if statement is not giving me the desired result.
Following is the code :

MainActivity.java

package com.example.login1; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { public EditText t1,t2; public Button b1; String user,pass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); t1=(EditText)findViewById(R.id.editText1); t2=(EditText) findViewById(R.id.editText2); b1= (Button) findViewById(R.id.button1); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View arg0) { user = t1.getText().toString(); pass = t2.getText().toString(); if (((user.equals("user")) && (pass.equals("user")))) { Toast.makeText(this, "Login Sucessful", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show(); t1.setText(""); t2.setText(""); } } } 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="fill_vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="29dp" android:layout_marginTop="28dp" android:text="Username" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:ems="10" android:inputType="text" > <requestFocus /> </EditText> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/textView1" android:layout_below="@+id/editText1" android:layout_marginTop="53dp" android:text="Password" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_centerVertical="true" android:ems="10" android:inputType="textPassword" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="86dp" android:clickable="true" android:onClick="onClick" android:text="Login" /> </RelativeLayout> 

While I run the App, on clicking the Login button, the if statement does not give a correct result.. I simply don't know where is the error.

9
  • please tell me the error, or output Commented Jan 20, 2014 at 19:16
  • 5
    String comparison should be done with .equals(), not ==. It should be if( user.equals("user") ). See this. Commented Jan 20, 2014 at 19:16
  • 1
    I think you have one more bracket than you need } Commented Jan 20, 2014 at 19:17
  • 1
    To expand on Roddy's comment, == tests whether 2 objects are the same object. .equals for String tests the value. Commented Jan 20, 2014 at 19:21
  • 1
    @k-mera: Edited it is!! Commented Jan 20, 2014 at 19:46

2 Answers 2

4

Well, you should compare strings with "equals()" and not with "==".

Replace:

user == "user" 

With:

user.equals("user") 

And also, I think there's no need for this extra line you have:

if (arg0.findViewById(R.id.button1)==b1) 

^ Why are you checking this?

Sign up to request clarification or add additional context in comments.

1 Comment

I was checking it weather the if statement after actually works on button press but then now it's clear since the xml documents contains the onClick method, so including in java code makes no sense now.
-1

use this code, for click command

b1 = (Button) findViewById(R.id.button1); b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { user = t1.getText().toString(); if (user.equals("user")){ Toast.makeText(this, "Login Sucessful", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show(); } } } 

1 Comment

Thanks for updating it. Much better. Just because the OP did it that way doesn't make it right.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.