How to check TextInputLayout if error is enabled

If editText1 is the focus and editTextWrapper is already showing an error and I move the frocus (to editText2 for example) the errorMessage will flicker a little (like focusListener turned on again) and there will be a slight animation glitch.

Is there a way to check if the TextInputLayout error is enabled (or another way to find out that the error is being displayed, other than intoucing for the outer class is valid for my class?

public class NoteCreateFragment extends Fragment{

@Bind(R.id.edit_text1) EditText editText1;
@Bind(R.id.edit_text2) EditText editText2;
@Bind(R.id.edit_text_wrapper) TextInputLayout editTextWrapper;


@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);


}


@Override
public View onCreateView(
    LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
){
    View view = inflater.inflate(R.layout.fragment_note_create, container, false);
    ButterKnife.bind(this, view);
    editText1.addTextChangedListener(textWatcher);
    editText1.setOnFocusChangeListener(focusListener);
    return view;
}


@Override
public void onDestroyView(){

    super.onDestroyView();
    ButterKnife.unbind(this);
}



private void createTask(){
    String text1 = editText1.getText().toString();
    if(text1.isEmpty()){

        editTextWrapper.setError("Please enter task name");
    } 
}





private TextWatcher textWatcher = new TextWatcher(){

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after){

    }


    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count){
        if(s.length() > 3){ editTextWrapper.setErrorEnabled(false); }
    }


    @Override
    public void afterTextChanged(Editable s){

    }
};
private View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener(){

    @Override
    public void onFocusChange(View v, boolean hasFocus){
        if(!hasFocus && editText1.getText().toString().length() < 1){
            editTextWrapper.setErrorEnabled(true);
            editTextWrapper.setError("Please enter task name");
            Log.e("TextInputLayout", editTextWrapper.getEditText().getError().toString());
        }
    }
};

      

}

+3


source to share


1 answer


Don't change setErrorEnabled()

at runtime to show / hide the error. Use setError(message)

to display the error and setError(null)

to hide the error. Thus,

if(!TextUtils.isEmpty(textInputLayout.getError())

      



returns true if the error is vissble, flase otherwise

+3


source







All Articles