2

I am trying to develop a login page but when a button is clicked it showing me username as ""

public class MainActivity extends AppCompatActivity {
public static final String userNameValue="abc";
public static final String passwordValue="abc";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EditText editText1=(EditText)findViewById(R.id.userName);
    final String valueUN= editText1.getText().toString();

    EditText editText2=(EditText)findViewById(R.id.password);
    final  String valuePW=editText2.getText().toString();

    Button loginButton=(Button)findViewById(R.id.button);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           if(valueUN==userNameValue && valuePW==passwordValue) {                
               Toast.makeText(MainActivity.this, "Login was Successfully", Toast.LENGTH_SHORT).show();
           } else {
               Toast.makeText(MainActivity.this, "Username or password is incorrect", Toast.LENGTH_SHORT).show();
           }
        }
    });
}
}

when I tried to debug the code using debugger it is displaying valueUN and valuePW as "",why it is showing so? it should display what I enter.

It is not duplicate of question Get Value of a Edit Text field cause in that question person wanted to capture value entered in a edittext and mmy question is that edit text is not displaying a right value

Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33
sainath pawar
  • 141
  • 2
  • 12
  • 3
    `valueUN==userNameValue` -> [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Sep 16 '17 at 19:40
  • yes I should have used ,equals() instead of ==it solved my problem – sainath pawar Sep 16 '17 at 19:53
  • Also i should write final String valueUN= editText1.getText().toString(); and final String valuePW= editText2.getText().toString();Inside on click() method – sainath pawar Sep 16 '17 at 19:55
  • It's a duplicate because you should look at the answer to that question and do it in the same way. – Oleg Sep 16 '17 at 20:02

1 Answers1

2

The problem is here

EditText editText1=(EditText)findViewById(R.id.userName);
final String valueUN= editText1.getText().toString();

You are storing value of username in valueUN at the same time it is created. You must understand that the time when edittext is created, its initial value is "". However, when user writes something on it and then performs any action for e.g. click on button in your case, at that time you should get the current value from edittext, not at the time it is created.

In your code, try getting value from edittext in onClickListener() method.

loginButton.setOnClickListener(new View.OnClickListener() {
    @Override

    public void onClick(View view) {

       String valueUN = editText1.getText().toString();
       String valuePW = editText2.getText().toString();
       if(valueUN.equals(userNameValue) && valuePW.equals(passwordValue)) {
           Toast.makeText(MainActivity.this, "Login was Successfully", Toast.LENGTH_SHORT).show();
       }
       else {
           Toast.makeText(MainActivity.this, "Username or password is incorrect", Toast.LENGTH_SHORT).show();
       }
    }
});

P.S.: Don't use == for String comparison. Instead use equals().

Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33