Search code examples
androiddata-bindingtextview

How to Get R.string in ViewModel Class of DataBinding in Android


I am currently using databinding for my android application project. I want to set the error message on my CustomTextView from R.string.txtOldPassWordError and set it up 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 string from strings.xml

<string name="select">Please Enter Old Password</string>

So, I want to access this string in my viewModel class and set it as error message instead of hardcoded it directly as string.

So how can i achieve this? I am using Android Studio 3.0 Beta Version. Any help would be really appreciated.


Solution

  • Create ResourceProvider class

    public class ResourceProvider {
    
        private Context mContext;
    
        public ResourceProvider(Context mContext) {
            this.mContext = mContext;
        }
    
        public String getString(int resId) {
            return mContext.getString(resId);
        }
    
        public String getString(int resId, String value) {
            return mContext.getString(resId, value);
        }
    }
    

    now go to your ApplicationClass and paste

    public class YourAppName extends Application {
      // Resource Provider
        private ResourceProvider mResourceProvider;
        public ResourceProvider getResourceProvider() {
            if (mResourceProvider == null)
                mResourceProvider = new ResourceProvider(this);
    
            return mResourceProvider;
        }
    }
    

    now go to your ChangePasswordViewModel and create object of ResourceProvider

    private ResourceProvider mResourceProvider;
    

    and pass it in constroctor of ChangePasswordViewModel

    than you can access it by

     userOldPasswordError.set(mResourceProvider.getString(R.string.select));