0

I am trying to show error when wrong value is inserted in edittext, below is my xml file

 <EditText
                android:id="@+id/BasicInfoDOBEditText"
                android:onClick="onCalenderClick"
                style="@style/TableRowSearchResultView"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:inputType="date" />

and how i am trying to show error is like this.

                dateEditText.setFocusable(true);
                dateEditText.requestFocus();
                dateEditText.setError("wrong input");

but it shows only error icon in edittext without "wrong input". I know there is some solution like extends EditText and override setError(), But is there some simple solution to it or i am doing something in wrong way. Please help !

FaisalAhmed
  • 3,469
  • 7
  • 46
  • 76

1 Answers1

0

to show error on editText i usually prefer TextInputLayout.

XML

<android.support.design.widget.TextInputLayout
    android:layout_margin="@dimen/margin_full"
    android:id="@+id/input_layout_new_todo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar_create_todo">

    <EditText
        android:id="@+id/et_new_todo"
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_todo_create_new"
        />

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

JAVA

private void getEtData(){
    title = todoEt.getText().toString();
    if(title.isEmpty() || title == null){
        inputLayoutHolder.setErrorEnabled(true);
        inputLayoutHolder.setError(getResources().getString(R.string.error_no_et_entry));
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            @Override
            public void run() {
                inputLayoutHolder.setErrorEnabled(false);
            }
        },1500);
    }else{
        title = todoEt.getText().toString();
        Log.i(TAG,"todo title: "+title);
    }
}

hope that helps

androCoder-BD
  • 498
  • 7
  • 13