I am developing an login and register app. The app is supposed to check if the entered data is correct or not while login or register. The login validation works fine but register doesn't validate data. The register button is supposed to check validations after clicking but nothing happens when I click it. There is no error shown in android studio and the app runs fine.
package com.example.investas;
import androidx.appcompat.app.AppCompatActivity;
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 android.widget.Toast;
public class RegisterActivity extends AppCompatActivity {
private EditText inputUsername,inputEmail,inputPassword,inputCpassword;
Button btnRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
TextView btn=findViewById(R.id.alreadyHaveAccount);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView btn=findViewById(R.id.alreadyHaveAccount);
inputUsername=findViewById(R.id.inputUsername);
inputEmail=findViewById(R.id.inputEmail);
inputPassword=findViewById(R.id.inputPassword);
inputCpassword=findViewById(R.id.inputCpassword);
btnRegister=findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
validations();
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
}
});
}
});
}
private void validations() {
String username=inputUsername.getText().toString();
String email=inputEmail.getText().toString();
String password=inputPassword.getText().toString();
String cpassword=inputCpassword.getText().toString();
if(username.isEmpty() || username.length()<7)
{
showError(inputUsername,"Your username is not valid");
}
else if(email.isEmpty() || !email.contains("@"))
{
showError(inputEmail,"Email is not valid");
}
else if(password.isEmpty() || password.length()<7)
{
showError(inputPassword,"Password must be 7 characters");
}
else if(cpassword.isEmpty() || !cpassword.equals(password))
{
showError(inputCpassword,"Passwords are not matching");
}
else
{
Toast.makeText(getApplicationContext(),"Registration
Successful",Toast.LENGTH_SHORT).show();
}
}
private void showError(EditText input, String your_username_is_not_valid) {
input.setError(your_username_is_not_valid);
input.requestFocus();
}
}
CodePudding user response:
Try to move the following lines out of the OnClickListener
, they should be in the onCreate
method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//...
inputUsername = findViewById(R.id.inputUsername);
inputEmail = findViewById(R.id.inputEmail);
inputPassword = findViewById(R.id.inputPassword);
inputCpassword = findViewById(R.id.inputCpassword);
btnRegister = findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
validations();
}
});
// ...
}
CodePudding user response:
Doing it this way is necessary for it to be cleaner and to work.
private EditText inputUsername,inputEmail,inputPassword,inputCpassword;
Button btnRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
TextView btn=findViewById(R.id.alreadyHaveAccount);
inputUsername=findViewById(R.id.inputUsername);
inputEmail=findViewById(R.id.inputEmail);
inputPassword=findViewById(R.id.inputPassword);
inputCpassword=findViewById(R.id.inputCpassword);
btnRegister=findViewById(R.id.btnRegister);
TextView btn=findViewById(R.id.alreadyHaveAccount);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
validations();
}
});
}