set Error via TextInputLayout - Android User Interface

Android examples for User Interface:Layout

Description

set Error via TextInputLayout

Demo Code


import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Main{
    public static void setError(View view, String error) {
        if (view instanceof EditText) {
            setError((EditText) view, error);
            return;
        }//  w  w w  .j  av a 2 s. co  m

        if (view instanceof TextView) {
            ((TextView) view).setError(error);
            return;
        }
    }
    private static void setError(EditText editText, String error) {
        try {
            ((TextInputLayout) editText.getParent()).setErrorEnabled(true);
            ((TextInputLayout) editText.getParent()).setError(error);
        } catch (ClassCastException e) {
            editText.setError(error);
        }
    }
}

Related Tutorials