Accessing Resource String In ViewModel Without Leaking Context

How To Access Resource String Without Using AndroidViewModel

Web & Mobile Development


Introduction

If you've been writing android code for a while, you've probably found yourself in a situation where you'd like to access resource strings in the ViewModel. The obvious solution to that for most people is to use the AndroidViewModel which gives you access to the application context. 

The Problem

The drawback of using the above approach is that it makes it difficult to unit test your ViewModel since you have to worry about mocking the android context, and, it won't scale well for apps that supports multiple languages because the ViewModel has a separate lifecycle from activities and fragments. 

If your app supports multiple languages, then you know that when your users change the language of your app, the activity will get recreated and its context will get updated. That way, the user can see the app texts in the new language. But keep in mind that the ViewModel is still holding a reference to the application context which doesn't get updated when the activity is being destroyed and recreated, because, the application context is always the same throughout the life of the current running process of the application. Thus the ViewModel application context will still be resolving string resources to the old language even though the user has updated the language to a new one. 

For the user to see the app texts in the new language, the user would have to kill the process of the app and then restart it. This way, the application context will obviously get updated. That's most likely not the kind of experience we would like our users to have while using our app. So how do we solve this problem in a clean manner?

The Solution

Create an extension function that will act as a wrapper for the property that needs the string resource in your view. For example, let's say we want to show error messages for our TextInputLayout. Then we would have,
fun TextInputLayout.setResError(resError: Int?) {
    resError?.let {
        setError(context.resources.getString(it))
    } ?: setError(null)
}


Then we will create a binding adapter to access the above extension function in our view xml code.
@BindingAdapter("app:errorRes")
fun setErrorMessage(view: TextInputLayout, resError: Int?): Unit {
    view.setResError(resError)
}


Then in our viewModel we will have 
class ExampleViewModel: ViewModel() {    
    val error = MutableLiveData<Int>()

    init {
       validateForm()
    }

    fun validateForm() {
       error.value = R.string.error_not_empty
    }
}



Finally, in our XML we will render our TextInputLayout as below
<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/field_password"
    app:errorRes="@{viewModel.error}"
    >

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>


How Does It Work?

Whenever an Int (which is a Resource String Int) value is emitted by the error property of the ViewModel, the binding adapter app:errorRes is called, which then calls the setResError extension function. The extension function will then use the view's context (which is the activity's context and not the application context) to resolve the string. The resolved string will then be used to set the value for the TextInputLayout error property. So whenever a user changes the language of the app and the activity context gets updated, you've got nothing to worry about anymore.

If you like being in an environment where you're allowed to be creative technically, consider joining our team at Oozou .

Looking for a new challenge? Join Our Team

Like 5 likes
Anu Karounwi
Android engineer at oozou
Share:

Join the conversation

This will be shown public
All comments are moderated

Get our stories delivered

From us to your inbox weekly.