There are many situations where we show Error when the user does not input right Data. Such as filling the form, User Login etc.
So, How we show an Error message, through DataBinding?
- Create EditText in our layout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<android.support.design.widget.TextInputLayout android:id="@+id/addBookTelephonelayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" app:error="@{billShipModel.BPhone.isEmpty() && billShipObsevable.billingShowError ? " Hii error find " : null}" android:layout_alignParentStart="true" android:layout_alignParentTop="true"> <EditText android:id="@+id/addBookTelephoneValue" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:hint="@string/telephone" android:text="@={billShipModel.BPhone}" android:inputType="phone" android:singleLine="true" android:textColorHint="@color/gray" /> </android.support.design.widget.TextInputLayout> |
- create field and getter/setter in our Model class
1 2 3 4 5 6 7 8 9 10 11 |
@Bindable public String getBPhone() { return bPhone; } public void setBPhone(String bPhone) { this.bPhone = bPhone; notifyPropertyChanged(BR.bPhone); if(this.bPhone.equalsIgnoreCase("")) getBillingShippingBinder().setBillingAddressValidation(false); } |
- create Observable for showingError
1 2 3 4 5 6 7 8 9 |
@Bindable public boolean isBillingShowError() { return billingShowError; } public void setBillingShowError(boolean billingShowError) { this.billingShowError = billingShowError; notifyPropertyChanged(BR.billingShowError); } |
- update showing error check when clicking on continue Button
1 2 3 4 5 6 7 8 9 10 11 12 |
public void onSaveBillingShippingInfoIv(View view, BillingShippingModel billShipModelData, BillingShippingBinder billShipObservable){ Log.d(TAG, "onSaveBillingShippingInfoIv: 1" + billShipObservable.isBillingAddressValidation()); billShipObservable.setBillingShowError(true); if(billShipModelData.isFormValidated()){ saveProfile(billShipModelData); }else{ Log.d(TAG, "onSaveBillingShippingInfoIv: 3"); billShipObservable.setBillingShowError(false); } } |