• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Login Demo - Programming Newbie

arpan_de

Lurker
Mar 30, 2020
2
0
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
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones