Home > Software engineering >  TextInputLayout error message keeps showing up even if the set condition result is false
TextInputLayout error message keeps showing up even if the set condition result is false

Time:11-05

Screenshot of the problem

This is the code is used for my application:

if (password.isEmpty() && confirmPassword.isEmpty()) {
            candidatePassword.setError("Field Cannot Be Empty");
            candidateConfirmPassword.setError("Field Cannot Be Empty");
        } else if (!password.equals(confirmPassword)) {
            candidatePassword.setError("Passwords Do Not Match");
            candidateConfirmPassword.setError("Passwords Do Not Match");
        } else {
            candidatePassword.setError(null);
            candidateConfirmPassword.setError(null);
        }

Actually, I have 2 versions of this. This one is for the "candidates", and the other is for the "voters". This piece of code works with no issues in the "voters" version of this. Can you point out what I am missing, and what need to do? Thank you!

This is the requested XML code for this activity:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="1dp"
    tools:layout_editor_absoluteY="1dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@ id/imageView5"
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_marginTop="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/university_of_makati_logo" />

        <TextView
            android:id="@ id/textView8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-condensed-medium"
            android:text="Candidacy Registration"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/imageView5" />

        <ImageView
            android:id="@ id/candidateImage"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="20dp"
            android:background="@color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/textView8"
            tools:srcCompat="@tools:sample/avatars" />

        <TextView
            android:id="@ id/clickableText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:fontFamily="sans-serif-condensed-medium"
            android:text="@string/clickable_text"
            android:textColor="@color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/candidateImage" />

        <TextView
            android:id="@ id/textView10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginTop="10dp"
            android:fontFamily="sans-serif-condensed-medium"
            android:text="Log-In Details"
            android:textColor="@color/white"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/clickableText" />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@ id/candidateEmail"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            app:errorEnabled="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/textView10">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed"
                android:hint="UMak Email Address"
                android:inputType="textEmailAddress" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@ id/candidatePassword"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            app:counterEnabled="true"
            app:counterMaxLength="25"
            app:counterTextColor="@color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/candidateEmail"
            app:passwordToggleEnabled="true">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed"
                android:hint="Create Password"
                android:inputType="textPassword" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@ id/candidateConfirmPassword"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            app:errorEnabled="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/candidatePassword"
            app:passwordToggleEnabled="true">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed"
                android:hint="Confirm Password"
                android:inputType="textPassword" />
        </com.google.android.material.textfield.TextInputLayout>

        <TextView
            android:id="@ id/textView9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginTop="10dp"
            android:fontFamily="sans-serif-condensed-medium"
            android:text="Candidate Details"
            android:textColor="@color/white"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/candidateConfirmPassword" />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@ id/candidateFullName"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            app:errorEnabled="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/textView9">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed"
                android:hint="Full Name"
                android:inputType="textCapSentences" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@ id/candidateYearSection"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            app:errorEnabled="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/candidateFullName">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed"
                android:hint="Year &amp; Section"
                android:inputType="textCapSentences" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@ id/candidatePosition"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            app:errorEnabled="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/candidateYearSection">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed"
                android:hint="Candidacy Position"
                android:inputType="textCapSentences" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@ id/candidateAchievements"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            app:errorEnabled="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/candidatePosition">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed"
                android:hint="Achievements Acquired"
                android:inputType="textCapSentences" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@ id/candidatePersonalQualities"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            app:errorEnabled="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/candidateAchievements">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed"
                android:hint="Personal Qualities"
                android:inputType="textCapSentences" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@ id/candidateBackground"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            app:errorEnabled="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/candidatePersonalQualities">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed"
                android:hint="Candidacy Background"
                android:inputType="textCapSentences" />
        </com.google.android.material.textfield.TextInputLayout>

        <CheckBox
            android:id="@ id/candidateCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:fontFamily="sans-serif-condensed"
            android:text="Information Provided is Relevant and Legitimate"
            android:textColor="@color/white"
            android:textStyle="italic"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/candidateBackground" />

        <Button
            android:id="@ id/candidateRegistrationButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="20dp"
            android:backgroundTint="@color/white"
            android:fontFamily="sans-serif-condensed-medium"
            android:text="Register"
            android:textColor="@color/black"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/candidateCheckBox" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

This is where I assigned my Strings:

private void registerCandidate() {
    try {
        String email = candidateEmail.getEditText().getText().toString().trim();
        String password = candidatePassword.getEditText().getText().toString();
        String confirmPassword = candidateConfirmPassword.getEditText().toString();
        String fullName = candidateFullName.getEditText().getText().toString().trim();
        String yearSection = candidateYearSection.getEditText().getText().toString().trim();
        String position = candidatePosition.getEditText().getText().toString().trim();
        String achievements = candidateAchievements.getEditText().getText().toString().trim();
        String personalQualities = candidatePersonalQualities.getEditText().getText().toString().trim();
        String background = candidateBackground.getEditText().getText().toString().trim();

CodePudding user response:

You have a small typo in the code where you get the String values.

It should be:

String password = candidatePassword.getEditText().getText().toString();
String confirmPassword = candidateConfirmPassword.getEditText().getText().toString();
  • Related