0

I am working on the Login page of an application. I want to compare a hard codded value to username , password given as input. Lines of code currently used is:

    usr = (EditText) findViewById(R.id.usr);   
      pwd = (EditText) findViewById(R.id.pwd);    
     error = (TextView) findViewById(R.id.tv_error);     
    String n1="i077653" ;  
      if(n1.equals(usr.getText().toString()) && pwd.equals("1234"))    
     {          error.setText("login success");     }         
     else     {      error.setText("LOGIN failed");         }
 .

But its not working .Can you suggest a solution for this ?

complete code : XML file :

XML file :<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: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="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="#FF6633"
        android:paddingLeft="60dp"
        android:paddingRight="100dp"
        android:text="@string/SA" />

    <TextView
        android:id="@+id/tv_pw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/pwd"
        android:layout_alignBottom="@+id/pwd"
        android:layout_alignLeft="@+id/tv_un"
        android:text="Password:"
        android:textColor="#444444"
        android:textSize="7pt" />

    <TextView
        android:id="@+id/tv_un"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/usr"
        android:layout_alignBottom="@+id/usr"
        android:layout_alignParentLeft="true"
        android:text="User Name:"
        android:textColor="#444444"
        android:textSize="7pt" />

    <EditText
        android:id="@+id/usr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_login"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="20dp"
        android:ems="7"
        android:hint="@string/User" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/pwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/usr"
        android:layout_below="@+id/usr"
        android:layout_marginTop="18dp"
        android:ems="7"
        android:hint="@string/Pwd"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pwd"
        android:layout_marginTop="18dp"
        android:layout_toRightOf="@+id/tv_un"

        android:text="@string/Button" />
<TextView
        android:id="@+id/tv_error"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:textSize="7pt"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/btn_login"
        android:layout_marginRight="9dip"
        android:layout_marginTop="15dip"
        android:layout_marginLeft="15dip"
        android:textColor="#AA0000"
        android:text=""/>



   </RelativeLayout>

Java file:

package com.example.survey;

import android.os.Bundle;

import android.app.Activity;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {
    EditText usr ;
    EditText pwd ;
    Button  log;
    TextView error;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


   public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void  check(View view){
    usr = (EditText) findViewById(R.id.usr);
    pwd = (EditText) findViewById(R.id.pwd);
    error = (TextView) findViewById(R.id.tv_error);
    String n1="i077653" ;


    if(n1.equalsIgnoreCase(usr.getText().toString()) && pwd.equalsIgnoreCase("1234"))    
    {          error.setText("login success");     }         
    else     {      error.setText("LOGIN failed");         }


    }
    }

4 Answers4

1

Try with

  if(n1.equalsIgnoreCase(usr.getText().toString()) && pwd.equalsIgnoreCase("1234"))    
     {          error.setText("login success");     }         
     else     {      error.setText("LOGIN failed");         }

but is block log in only if User=i077653 AND password=1234 if u want "if User=i077653 OR password=1234" change && with ||

Lele
  • 703
  • 3
  • 15
  • 34
  • Ok guys all your answers isn't working and i have already tried them out . Do you have an alternative approach or code that i can use ?? – Gaganpreet Singh Jun 20 '13 at 10:25
1

Change your code to:

  if(n1.equals(usr.getText().toString()) && (pwd.getText().toString()).equals("1234"))    

Here pwd is the edit text variable. It contains its refferance not the entered value.

Edit:()

public class MainActivity extends Activity { EditText usr ; EditText pwd ; Button log; TextView error;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    usr = (EditText) findViewById(R.id.usr);
    pwd = (EditText) findViewById(R.id.pwd);
    error = (TextView) findViewById(R.id.tv_error);
    log=(Button) findViewById(R.id.tv_log);
    String n1="i077653" ;

   log.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

             if(n1.equalsIgnoreCase(usr.getText().toString()) &&(pwd.getText().toString()).equalsIgnoreCase("1234"))   
     { 

     error.setText("login success");     

     }         
     else{

     error.setText("LOGIN failed"); 

     }
    }

   });

    }//end of oncreate
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    }

Advice: Please follow this tutorial

amalBit
  • 12,041
  • 6
  • 77
  • 94
  • Ok guys all your answers isn't working and i have already tried them out . Do you have an alternative approach or code that i can use ?? – Gaganpreet Singh Jun 20 '13 at 10:26
0
String n1="i077653" ;  
  if(n1.equals(usr.getText().toString()) && pwd.equals("1234"))    
 {          error.setText("login success");     }         
 else     {      error.setText("LOGIN failed");         }

Try this code at the time on some event like onclick or some other event

dharmendra
  • 7,835
  • 5
  • 38
  • 71
0
 if(n1.equals(usr.getText().toString()) && "1234".equals(pwd.getText().toString())){

   // login success
}else{
 ...
}
liuzc
  • 26
  • 1
  • Ok guys all your answers isn't working and i have already tried them out . Do you have an alternative approach or code that i can use ?? – Gaganpreet Singh Jun 20 '13 at 10:26