1. Are you ready for the Galaxy S20? Here is everything we know so far!

Login Demo - Programming Newbie

Discussion in 'Android Lounge' started by arpan_de, Mar 30, 2020.

  1. arpan_de

    arpan_de Lurker
    Thread Starter

    I am trying to create a small Android login app using the following code:

    package com.example.logindemo;

    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    import androidx.appcompat.app.AppCompatActivity;

    public class MainActivity extends AppCompatActivity {

    private EditText Name;
    private EditText Password;
    private TextView Info;
    private Button Login;
    private int counter=5;
    private TextView Alert;

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

    Name=(EditText)findViewById(R.id.etName);
    Password=(EditText)findViewById(R.id.etPassword);
    Info=(TextView)findViewById(R.id.tvInfo);
    Login=(Button)findViewById(R.id.btnLogin);
    Alert=(TextView)findViewById(R.id.tvAlert);

    Info.setText("No. of attempts remaining: 5");

    Login.setOnClickListener(new View.OnClickListener(){
    @override
    public void onClick(View view){
    validate(Name.getText().toString(),Password.getText().toString());
    }
    });
    }
    private void validate(String userName, String userPassword){
    Alert.setText(userName + " - " + userPassword);
    if((userName=="admin") && (userPassword=="1234")){
    Intent intent=new Intent(MainActivity.this,SecondActivity.class);
    startActivity(intent);
    }else{
    counter--;
    Info.setText("No. of attempts remaining: " + String.valueOf(counter));
    if(counter==0){
    Login.setEnabled(false);
    }
    }
    }
    }

    The problem is when I enter "admin" as the username & "1234" as the password, I don't get logged in & instead the counter gets decremented by 1. Where am I going wrong?

    Thanks
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...