2

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); } }); } } } 
0

3 Answers 3

1

Following @Jimmy's answer, you also need to modify your else-if condition, since only when userName, password and confirmPassword were not empty string, the condition enterRequiredFields should be made false.

From,

 else if(!userName.equals("") || !password.equals("") || !confirmPassword.equals("")){ enterRequiredFields = false; } 

To,

 else if(!userName.equals("") && !password.equals("") && !confirmPassword.equals("")){ enterRequiredFields = false; } 
Sign up to request clarification or add additional context in comments.

Comments

1

That's because the block of code that evaluates enterRequiredFields is inside the OnCreate block - that means it is evaluated on start up to true since nothing has been entered yet, and it never changes.

You should simply move the block that evaluates enterRequiredFields and passwordMatching inside the onClick for createButton, so that these are evaluated when the user clicks the button, and it should then work as expected.

UPDATE You need to bring the references for userName password and confirmPassword into the OnClick block too, like this:

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; @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) { String userName = edtUsername.getText().toString(); String password = edtPassword.getText().toString(); String confirmPassword = edtConfirmPassword.getText().toString(); boolean enterRequiredFields = false; boolean passwordMatching = true; 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); } }); } } } 

4 Comments

The App is crashing after adding block of coide inside onClick() method.The following message displays "Unfortunately your app has stopped",although no error is shown inside Android Studio.
Yeah sure.I have posted updated code by editing my previous question.Please check it.
Thanks - you need to pull out the text inside each of the edittext controls within the onclick block, I have posted the updated code based on yours, try and see if that works? If not, please provide the error provided by your IDE (logcat if Android Studio)
Thanks bro.It's working perfectly.Besides that I used AND operation inside my if-else statements as I wanted to show toast message even if one of the fields was empty.Now I have figured out my mistake.
0

So instead of cheacking your edittexts in onCreate, do it this way. Also use TextUtils to get accurate empty string checks from Edittext like this.

 createButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(TextUtils.isEmpty(userName)|| TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)){ enterRequiredFields = true; }else if(!TextUtils.isEmpty(userName) || !TextUtils.isEmpty(password) || !TextUtils.isEmpty(confirmPassword)){ 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); } } }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.