Search code examples
androidandroid-edittexthint

Android hint is hindering EditText validation


I have 2 questions.

I have a few EditTexts that have to be validated on a button click. I used everything to check if the edittexts are empty, but it always showed I have something in edittext and is not empty. I later removed the hint and it worked.

I tried edittext.getText().toString().trim().length()==0, TextUtils.isEmpty(edittext.getText()), edittext.matches(""), but nothing worked.

What should I do to check if the EditTextis empty without removing the android:hint="blah"

2.And also, I'm trying to setError for the editName,editPhNo. But it's not displayed. It works for the editPassword,editConfirmPassword. Is there something wrong with my layout?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.prematixsofs.taxiapp.EditUserDetails">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"

    >
    <!--Display UserDetails Layout-->

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/displayLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="15dp">

            <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:background="@drawable/custom_edittext"
                android:gravity="center"
                android:padding="8dp"
                android:paddingLeft="10dp"
                android:paddingRight="5dp" />

            <TextView
                android:id="@+id/phNo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@drawable/custom_edittext"
                android:gravity="center"
                android:padding="8dp"
                android:paddingLeft="10dp"
                android:paddingRight="5dp" />

            <TextView
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@drawable/custom_edittext"
                android:gravity="center"
                android:inputType="textEmailAddress"
                android:padding="8dp"
                android:paddingLeft="10dp"
                android:paddingRight="5dp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="15dp">

            <Button
                android:id="@+id/edit"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:background="@drawable/button_custom"
                android:text="Edit"
                android:textColor="#ffffff" />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

<!--Edit Layout -->
<LinearLayout
    android:id="@+id/editLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    >

    <EditText
        android:id="@+id/editName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/custom_edittext"
        android:gravity="center"
        android:hint="Enter Name"
        android:padding="8dp"
        android:paddingLeft="10dp"
        android:paddingRight="5dp" />

    <EditText
        android:id="@+id/editPhNo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/custom_edittext"
        android:gravity="center"
        android:hint="Enter new Phone Number"
        android:padding="8dp"
        android:paddingLeft="10dp"
        android:paddingRight="5dp" />

    <EditText
        android:id="@+id/editPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/custom_edittext"
        android:gravity="center"
        android:hint="Enter new Password"
        android:padding="8dp"
        android:paddingLeft="10dp"
        android:paddingRight="5dp"
        android:password="true" />

    <EditText
        android:id="@+id/editConfirmPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/custom_edittext"
        android:gravity="center"
        android:hint="Confirm Password"
        android:padding="8dp"
        android:paddingLeft="10dp"
        android:paddingRight="5dp"
        android:password="true" />

    <Button
        android:id="@+id/saveOnEdit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="15dp"
        android:layout_marginTop="60dp"
        android:background="@drawable/button_custom"
        android:gravity="center"
        android:text="Save"
        android:textColor="#ffffff" />

</LinearLayout>

saveOnEdit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {



if(TextUtils.isEmpty(name.getText())&&TextUtils.isEmpty(phno.getText())&&TextUtils.isEmpty(editPassword.getText())&&TextUtils.isEmpty(editConfirmPassword.getText())) //Check if empty here
            {
                Toast.makeText(getApplicationContext(),"Enter the details you want to update",Toast.LENGTH_LONG).show();
            }
            else {
                if (validate()) {
                    if (editConfirmPassword.getText().toString().equals(editPassword.getText().toString())) {
                        databaseHelper.saveOnEdit(sessionManager.getUserEmail(), editName.getText().toString(), editPhNo.getText().toString(), editPassword.getText().toString().toString());
                        editName.setText("");
                        editConfirmPassword.setText("");
                        editPassword.setText("");
                        editPhNo.setText("");
                    } else {
                        editConfirmPassword.setError("Passwords don't match");
                        editPassword.setError("Passwords don't match");
                        Toast.makeText(getApplicationContext(), "Passwords don't match", Toast.LENGTH_LONG).show();
                    }
                } else {
                    phno.setError("Enter valid phone number");
                    Toast.makeText(getApplicationContext(), "Enter a valid Phone Number", Toast.LENGTH_LONG).show();
                }
            }
        }
    });

super.onCreate(savedInstanceState); setContentView(R.layout.activity_edit_user_details); edit = (Button) findViewById(R.id.edit); name = (TextView) findViewById(R.id.name); phno = (TextView) findViewById(R.id.phNo); email = (TextView) findViewById(R.id.email); editName = (EditText) findViewById(R.id.editName); editPassword = (EditText) findViewById(R.id.editPassword); editConfirmPassword = (EditText) findViewById(R.id.editConfirmPassword); editPhNo = (EditText) findViewById(R.id.editPhNo); userDetailList = new ArrayList(); displayLayout = (LinearLayout) findViewById(R.id.linearLayout); editLayout = (LinearLayout) findViewById(R.id.editLayout); editLayout.setVisibility(editLayout.GONE); databaseHelper = new DatabaseHelper(getApplicationContext()); sessionManager = new SessionManager(getApplicationContext()); userDetailList = databaseHelper.displayUserDetails(sessionManager.getUserEmail()); name.setText(userDetailList.get(0).toString()); phno.setText(userDetailList.get(1).toString()); email.setText(userDetailList.get(2).toString()); displayLayout = (LinearLayout) findViewById(R.id.linearLayout); editLayout = (LinearLayout) findViewById(R.id.editLayout); saveOnEdit = (Button) findViewById(R.id.saveOnEdit);


Solution

  • Based on your edit to the question, you are checking the wrong fields! name and phno are TextView. Change them to the actual EditText as shown below:

    if(TextUtils.isEmpty(editName.getText())&&TextUtils.isEmpty(editPhNo.getText())&&TextUtils.isEmpty(editPassword.getText())&&TextUtils.isEmpty(editConfirmPassword.getText())) //Check if empty here
    {
        Toast.makeText(getApplicationContext(),"Enter the details you want to update",Toast.LENGTH_LONG).show();
    }