I am currently using databinding for my Android app project. I want to set the error message on my CustomTextView from R.string.txtOldPassWordError and set it from another class called ViewModelClass .
Here is my xml code
<com.horseproject.widget.CustomEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginTop="@dimen/dp_20" android:drawablePadding="@dimen/dp_10" android:hint="@string/enter_old_password" android:inputType="textPassword" android:lines="1" android:text="@={ChangePasswordVM.userOldPassword}" android:textColorHint="@color/gray_text" app:drawableLeftCompat="@drawable/ic_password_lock" app:drawableTintCompat="@color/gray_app" app:error="@{ChangePasswordVM.userOldPasswordError}" />
and this is ViewModelClass.java
public class ChangePasswordViewModel extends BaseObservable { public ObservableField<String> userOldPassword = new ObservableField<>(""); public void userPasswordChange() { if ((TextUtils.isEmpty(userOldPassword.get()))) { userOldPasswordError.set("Please enter your old password"); return; } else if (userOldPassword.get().length() <= 5) { userOldPasswordError.set("Password should contain minimum 6 characters"); return; } else { userOldPasswordError.set(null); } } }
and this is the string from strings.xml
<string name="select">Please Enter Old Password</string>
So, I want to access this line in my viewModel class and set it as an error message instead of hardcoded it directly as string .
So how can I achieve this? I am using beta version of Android Studio 3.0. Any help would be really appreciated.
user8536938
source share