Hey I am a beginner to android studio and I have just started building android apps.Nowadays I am building an app using java in which user is required to create an account before using app.If user click Create Button and the required fields are empty or if the password and re-enter password don't match,respective toast messages have to be displayed on the screen.But if neither of those conditions happen,I simply want the user to move on to next Activity(on clicking Button).But the problem is when I tested the app,I entered all the required information in given fields and both passwords were also correct but instead of displaying next activity,my app was showing first toast message repeatedly.I' have been stuck here for a long time.I have tried to change if-else statements too but nothing worked.Can anybody help me how to run my app withput these issues?My code is listed below
package com.example.oneclickscanner; 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 AccountActivity extends AppCompatActivity { TextView accountLogo; EditText edtUsername,edtPassword,edtConfirmPassword; Button createButton,exitButton; String userName = edtUsername.getText().toString(); String password = edtPassword.getText().toString(); String confirmPassword = edtConfirmPassword.getText().toString(); boolean enterRequiredFields = false; boolean passwordMatching = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_account); accountLogo = findViewById(R.id.accountLogo); edtUsername = findViewById(R.id.edtUsername); edtPassword = findViewById(R.id.edtPassword); edtConfirmPassword = findViewById(R.id.edtConfirmPassword); createButton = findViewById(R.id.createButton); exitButton = findViewById(R.id.exitButton); createButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(userName.equals("") || password.equals("") || confirmPassword.equals("")){ enterRequiredFields = true; }else if(!userName.equals("") || !password.equals("") || !confirmPassword.equals("")){ enterRequiredFields = false; } if(!password.equals(confirmPassword)){ passwordMatching = false; }else{ passwordMatching = true; } if(enterRequiredFields){ Toast.makeText(AccountActivity.this,"Please Enter the required Fields",Toast.LENGTH_SHORT).show(); }else if(!passwordMatching) { Toast.makeText(AccountActivity.this,"Password don't Match",Toast.LENGTH_SHORT).show(); }else { Intent intent = new Intent(AccountActivity.this,ModelAndLicenseInfo.class); startActivity(intent); } } }); exitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(AccountActivity.this,MainActivity.class); startActivity(intent); } }); } } }