2

I'm very new to Android programming so please forgive me if this is a stupid query.

I have an Edittext for a password field, and a button to submit the password. I also have a couple of Textviews which are hidden (shows if password is correct/incorrect), which I'd like to turn visible using an if/else statement.

When I run the code (on an AVD), nothing happens when I click the submit button. At all, ever.

I know this is probably really simple, but looking at other answers on questions similar to this was no help, I've been trying for days to figure out what I'm doing wrong...

..So ANY help would be really really appreciated. Thank you!! :D

-I use Eclipse, and Android 4.2.2

Here's the Java part:

final TextView success = (TextView) findViewById(R.id.textView1);
final TextView failure = (TextView) findViewById(R.id.textView2);
Button firstButton = (Button)findViewById(R.id.button1);
final EditText userPassword  = (EditText)findViewById(R.id.passwordField);
firstButton.setOnClickListener(
new View.OnClickListener(){
    public void onClick(View v){ 
        if(userPassword.getText().toString() == "pa$$word"){
            success.setVisibility(View.VISIBLE);
        }
        else{
           failure.setVisibility(View.VISIBLE);
        }
    }
});

XML:

<Button android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:text="@string/button" 
android:layout_marginBottom="75dp" 
android:layout_centerHorizontal="true" 
android:layout_alignParentBottom="true" 
android:id="@+id/button1"/> 

<EditText android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:layout_centerHorizontal="true" 
android:id="@+id/passwordField" 
android:inputType="textPassword" 
android:ems="10" 
android:layout_above="@+id/button1"> 

<requestFocus/> 
</EditText> 
<TextView android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:text="@+string/pwCorrect"
 android:layout_marginBottom="124dp"
 android:layout_centerHorizontal="true"
 android:id="@+id/textView1"
 android:layout_above="@+id/passwordField"
 android:visibility="gone"/> 

<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content" 
android:text="@+string/pwIncorrect" 
android:layout_centerHorizontal="true" 
android:id="@+id/textView2" 
android:visibility="gone" 
android:layout_centerVertical="true"/>
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
newbie
  • 31
  • 5

5 Answers5

2
 if(userPassword.getText().toString() == "pa$$word"){

Strings in java have to compared with equals or equalsIgnoreCase. Using == you will compare the address of the String

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
2

Compare string with equals or equalsIgnoreCase

try this

if(userPassword.getText().toString().equals("pa$$word"))
Pragnani
  • 20,075
  • 6
  • 49
  • 74
0

When comparing strings in Java you want to use the following syntax:

if(userPassword.getText().toString().equals("pa$$word"))

Also, I don't think the final is necessary on your EditTexts.

TronicZomB
  • 8,667
  • 7
  • 35
  • 50
0

Use this, sometime password are in alphabet with caps-lock so better use equalsIgnoreCase

if(userPassword.getText().toString().equalsIgnoreCase("pa$$word"))
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
0

use this code snippet

if(userPassword.getText().toString().equalsIgnoreCase("your valid passwor")){

    // do something 

}else{

 //wrong password

}

enjoy:) Please don't forget to give +1

Deepak Sharma
  • 4,999
  • 5
  • 51
  • 61