AndiGeeky
AndiGeeky

Reputation: 11464

Only Error icon is visible while setting error to EditText in InputTextLayout

When user enter invalid email or mobile while registration, I am setting error to EditText.

Please check below code:

    <android.support.design.widget.TextInputLayout
        android:id="@+id/txt_input_mobile_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/input_mobile_number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Mobile number"
            android:maxLength="10"
            android:inputType="number"
            android:maxLines="1"/>

    </android.support.design.widget.TextInputLayout>

MyFragment.java implements below OnKeyListener:

 @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
            // perform validation of data here
            switch (v.getId()) {

                case R.id.input_mobile_number:
                    EditText editMobile = (EditText)getActivity().findViewById(R.id.input_mobile_number);
                    Editable phone = binding.inputMobileNumber.getText();
                    if (phone != null && isValidMobile(phone.toString())) {
                        editMobile.setError("Please enter valid mobile number");
                        editMobile.requestFocus();
                    }
                    break;
            }
            return true;
        }
        return false;
    }

Below screenshot displays, Only error icon is visible not error box like other one:

with only symbol enter image description here

with error prompt

enter image description here

Upvotes: 0

Views: 854

Answers (3)

Nickan
Nickan

Reputation: 466

Maybe the space between these two TextInputLayout is low and you cannot see the error message , try to put more margin between them.

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

You should use TextInputLayout instead of EditText for your Requirement .

TextInputLayout tilObj = (TextInputLayout) findViewById(R.id.txt_input_mobile_number);
tilObj .setErrorEnabled(true);
tilObj .setError("Please enter valid mobile number");

Upvotes: 2

AndiGeeky
AndiGeeky

Reputation: 11464

Figured out!!

When we use TextInputLayout we need to set error not to EditText but TextInputLayout itself.

So I have changed my code as below:

  @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                // perform validation of data here
                switch (v.getId()) {

                    case R.id.input_mobile_number:
                      // check it is TextInputLayout not EditText
                        TextInputLayout editMobile = (EditText)getActivity().findViewById(R.id.txt_input_mobile_number);
                        Editable phone = binding.inputMobileNumber.getText();
                        if (phone != null && isValidMobile(phone.toString())) {
                            editMobile.setError("Please enter valid mobile number");
                            editMobile.requestFocus();
                        }
                        break;
                }
                return true;
            }
            return false;
        }

Upvotes: 1

Related Questions