android - How to set error on EditText using DataBinding Framwork MVVM -


hello using android data binding framework have suppose edittext login form username below

<edittext         android:id="@+id/etext_uname"         style="@style/login_edittext"         android:hint="@string/hint_username"         android:inputtype="textemailaddress" /> 

i have defined loginviewmodel need how set error in edittext when user type wrong email address in event let inside

public void aftertextchanged(@nonnull final editable editable) 

because far know in traditional android approach can programmatically via et.seterror() method don't want create edittext object via activity or fragment.

if want edittext.seterror() function databinding, here 2 method.

method 1

used final edittext view generated data binding (https://developer.android.com/topic/libraries/data-binding/index.html#views_with_ids)

you can call edittext directly without creating manually since automatically generated after set id view (also true included layout) .

mainactivitybinding.etext_uname.seterror("wrong email format"); 

or

mainactivitybinding.etext_uname.addtextchangedlistener(new myowntextwatcher()); 

method 2

if want use binding method xml george mentioned (https://medium.com/google-developers/android-data-binding-custom-setters-55a25a7aea47#.su88ujqrn)

first have set own binding method. suggest create class binding method.

method must static, @bindingadapter annotation , corresponding binding method name (namespace , method name can customized)

1. set custom textwatcher

public class myownbindingutil {     public interface stringrule {         public boolean validate(editable s);     }     @bindingadapter("android:watcher")     public static void bindtextwatcher(edittext pedittext, textwatcher ptextwatcher) {         pedittext.addtextchangedlistener(ptextwatcher);     }     @bindingadapter(value = {"email:rule", "email:errormsg"}, requireall = true)     public static void bindtextchange(final edittext pedittext, final stringrule pstringrule, final string msg) {         pedittext.addtextchangedlistener(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) {             }             @override             public void aftertextchanged(editable s) {                 if (!pstringrule.validate(s)) {                     pedittext.seterror(msg);                 }             }         });     }     /*     other custom binding method      */ } 

if want setup own textwatcher custom action, toast shown, dialog shown. should use "android:watcher" method

mbinding.setwatcher(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) {     }     @override     public void aftertextchanged(editable s) {     } }); 

in xml,

<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:email="http://schemas.android.com/tools"     >      <data>         <variable             name="watcher"             type="android.text.textwatcher"/>         <variable             name="emailrule"             type="example.com.testerapplication.myownbindingutil.stringrule"/>         <variable             name="errormsg"             type="java.lang.string"/>     </data>     <edittext         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:hint="input email"         android:watcher="@{watcher}         /> 

2. setup own validation rule , error msg

if want use seterror function , left errormsg , validation logic customized. can set xml following.

in xml,

<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:email="http://schemas.android.com/tools"     >      <data>         <variable             name="watcher"             type="android.text.textwatcher"/>         <variable             name="emailrule"             type="example.com.testerapplication.myownbindingutil.stringrule"/>         <variable             name="errormsg"             type="java.lang.string"/>     </data>     <edittext         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:hint="input email"         email:rule="@{emailrule}"         email:errormsg="@{errormsg}"         /> 

activity code

mbinding.seterrormsg("wrong type"); mbinding.setemailrule(new myownbindingutil.stringrule() {     @override     public boolean validate(editable s) {         // check if length of string larger 18           return s.tostring().length() > 18;     } }); 

please feel free edit code make binding more generic developer use.


Comments