in Education by
I am using Android Data Binding framework I have suppose an EditText for login form with username as below I have defined LoginViewModel also but I need help how to set Error in edittext when user type wrong email address in some event let say inside public void afterTextChanged(@NonNull final Editable editable) Because as far as I know in Traditional Android approach we can do this programmatically via et.setError() method but I don't want to create edittext object via Activity or Fragment. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
If you want to do something like EditText.setError() function with databinding, here is two method. Method 1 Used the final EditText view generated from the data binding (https://developer.android.com/topic/libraries/data-binding/index.html#views_with_ids) You can call the EditText directly without creating it manually since it is automatically generated after you set the id for the view (also true for the included layout) . MainActivityBinding.etext_uname.setError("Wrong email format"); Or MainActivityBinding.etext_uname.addTextChangedListener(new MyOwnTextWatcher()); Method 2 If you want to use the binding method with xml as George mentioned (https://medium.com/google-developers/android-data-binding-custom-setters-55a25a7aea47#.su88ujqrn) First you have to set your own binding method. Suggest to create another class for all the binding method. Method must be static, with @BindingAdapter annotation and the corresponding binding method name (Namespace and the method name can be customized) 1. Set the 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); } } }); } /* Your other custom binding method */ } If you want to setup your own TextWatcher with custom action, like Toast shown, Dialog shown. You 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"?> 2. Setup your own validation Rule and error Msg If you want to use setError function and only left the errorMsg and validation logic to be customized. You can set the xml like the following. In xml, <?xml version="1.0" encoding="utf-8"?> Activity code mBinding.setErrorMsg("Wrong type"); mBinding.setEmailRule(new MyOwnBindingUtil.StringRule() { @Override public boolean validate(Editable s) { // check if the length of string is larger than 18 return s.toString().length() > 18; } }); Please feel free to edit my code to make the binding be more generic for the developer use.

Related questions

0 votes
    At first I made an application that downloads a file from the entered link and displays information about ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    I want to automatically show the soft-keyboard when an EditText is focused (if the device does not have a physical ... not present a soft-keyboard if there is a physical one....
asked Mar 9, 2021 in Technology by JackTerrance
0 votes
    I have a custom class (NewBlockLabelInfo) with an observable collection of another custom class ( ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I have a screen that has textviews and edittexts. I am trying to edittexts equal width. But they look ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I sometimes run into Fatal error: require() [function.require]: apc_fcntl_lock failed errno:6 in C:\web\ ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    I create a project with "dotnet new angular". When a breakpoint is set on a Typescript instruction, it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I hava a main view, where you have one object and you can change the properties of that object. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    How can you explain view and view model in MVVM?...
asked Apr 8, 2021 in Education by JackTerrance
0 votes
    How can i apply MVVM pattern in QtQuick applications? Can anybody give me any sample (simple) code? Thanks ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    What is MVVM architecture in Angular?...
asked Jun 30, 2021 in Technology by JackTerrance
0 votes
    Suppose you have designed a Big Data batch using the MapReduce framework. Now you want to execute it on a cluster ... Run view? Name Node Data Node Resource Manager Job Tracker...
asked Mar 23, 2021 in Technology by JackTerrance
0 votes
    My application uses SAF (jsr 296). I want to use different look and feels on different platforms: A ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    after installing composer I tried to install larvel but it gives ErrorException .please help.... data from ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
...