Im creating a simple login activity and am implementing some user input error checking on some EditText fields.
However, the setError public method seems to not show anything or, if it is, I'm unable to view when running the app.
XML for one of the EditText elements
<EditText
android:id="@ id/userName"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="@ id/imageView"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="25dp"
android:layout_marginTop="35dp"
android:layout_marginEnd="25dp"
android:background="#FFFFFF"
android:elevation="10dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
android:minHeight="48dp"
android:textColor="@color/black"
android:textColorHint="#757575"
android:textSize="16sp" />
Java for error checking logic and account creation
public class NewUserActivity extends AppCompatActivity {
EditText mUserName, mUserEmail , mUserPassword, mReEnterPass;
//CardView cdCreateAcct;
Button btnCreateAcct;
TextView btnGoToLogin;
FirebaseAuth firebaseAuth;
ProgressBar progressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_user);
//java variables connecting to the xml objects/resources
mUserName = findViewById(R.id.userName);
mUserEmail = findViewById(R.id.userEmail);
mUserPassword =findViewById(R.id.userPassword);
mReEnterPass=findViewById(R.id.reEnterPass);
btnCreateAcct = findViewById(R.id.createAcctBtn);
btnGoToLogin = findViewById(R.id.alreadyMadeAcctText);
//instantiate the firebase variable
//getting the current instance of database from firebase
// to perform actions on the database
firebaseAuth = FirebaseAuth.getInstance();
//progress bar
progressBar = findViewById(R.id.progressBar);
//checking if user is already logged in or account already exists
if(firebaseAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}
btnCreateAcct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Display a toast message
//Toast.makeText(NewUserActivity.this, "You clicked card view ", Toast.LENGTH_SHORT).show();
String name = mUserName.getText().toString().trim();
String email = mUserEmail.getText().toString().trim();
String password =mUserPassword.getText().toString().trim();
String reEnterP = mReEnterPass.getText().toString().trim();
//logic to check inputs , null , passwords the same ect.
if (TextUtils.isEmpty(name)){
mUserName.requestFocus();
mUserName.setError("Enter in a Username.");
//mUserName.setError("Enter in a User Name.",);
return;
}
if (TextUtils.isEmpty(email)){
mUserEmail.requestFocus();
mUserEmail.setError("Email is Required.");
return;
}
if (TextUtils.isEmpty(password)){
mUserPassword.requestFocus();
mUserPassword.setError("Password is Required");
return;
}
if (password.length() <= 8){
mUserPassword.requestFocus();
mUserPassword.setError("Passwords must be Greater than 8 Characters.");
}
if (TextUtils.isEmpty(reEnterP)){
mReEnterPass.requestFocus();
mReEnterPass.setError("Passwords must be the Same.");
return;
}
if((!password.equals(reEnterP))) {
mReEnterPass.requestFocus();
mReEnterPass.setError("Passwords must be the Same.");
return;
}
//if everything is passed progress bar will show
progressBar.setVisibility(View.VISIBLE);
//create account in firebase
firebaseAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(NewUserActivity.this, "User Created", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
}
else{
Toast.makeText(NewUserActivity.this, "Error !" task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
Even when clicking my Create account Button and leaving Username blank I see no error message. The edittext field is null and shows no error
Is my error checking logic off, or is there something wrong in my XML?
CodePudding user response:
Another solution you could try is making your EditText focusable with 2 properties :
android:focusable="true"
android:focusableInTouchMode="true"
Example :
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world" />
Source : Android setError("error") not working in Textview
CodePudding user response:
It was an issue with my Android manifest , must declare the right activity as the launcher.