Sunday, 25 August 2013

Why is only 'else' part getting executed, even if 'if' is true?

Why is only 'else' part getting executed, even if 'if' is true?

Here is my main activity:
package com.santosh.sampleapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
public class Main extends Activity
{
EditText password_field;
TextView tv_err;
Button login;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// the password of the app
password_field = (EditText) findViewById(R.id.password_field);
tv_err = (TextView)findViewById(R.id.tv_err);
login = (Button)findViewById(R.id.login);
final String password = password_field.getText().toString();
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (password.equals("password")) {
tv_err.setText("good pass");
} else {
tv_err.setText("wrong pass");
}
} catch (Exception e) {
tv_err.setText(e.toString());
}
}
});
}
}
Even if I type password at the textview, it shows me a wrong pass message.
Here is my src/layout/main.xml if it helps:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:id="@+id/password_field"
android:hint="@string/password_hint"
android:inputType="textPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:id="@+id/login"
android:text="@string/get_message"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="launchAfterPassword"
/>
<TextView android:id="@+id/tw_err"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout>

No comments:

Post a Comment