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.

linthum
  • 333
  • 1
  • 8
  • 22

2 Answers2

5

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?

CodingDuckling
  • 595
  • 9
  • 20
  • 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. – linthum Jan 20 '14 at 19:37
-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();
        }
    }
}
chtenb
  • 14,924
  • 14
  • 78
  • 116
Ashish Ratan
  • 2,838
  • 1
  • 24
  • 50