OOZOU
Get in Touch
Back to Blog

Accessing Resource String In ViewModel Without Leaking Context

How To Access Resource String Without Using AndroidViewModel

December 19, 2020
3 min read
Accessing Resource String In ViewModel Without Leaking Context

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?.<em>let </em><strong>{
        </strong>setError(<em>context</em>.resources.getString(<strong>it</strong>))
    <strong>} </strong>?: 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.<em>setResError</em>(resError)
}

Then in our viewModel we will have

class ExampleViewModel: ViewModel() {<pre>    
    <strong>val error = MutableLiveData<Int>()
</strong>
    init {
       validateForm()
    }

    fun validateForm() {
       <strong>error.value = R.string.error_not_empty</strong>
    }
}

Finally, in our XML we will render our TextInputLayout as below

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/field_password"
    <strong>app:errorRes="@{viewModel.error}"</strong>
    >

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

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

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 .

Related Articles

How Can I Design a Software: A Beginners Guide for Business Owners

How Can I Design a Software: A Beginners Guide for Business Owners

Designing software is an exciting yet complex process that involves creativity, logical thinking, and problem-solving skills.

Read More →
The Future of Edge Computing: What Businesses Need to Know

The Future of Edge Computing: What Businesses Need to Know

The digital landscape is evolving rapidly, and businesses must adapt to keep up with rising demands for speed, efficiency, and real-time processing.

Read More →
The Future of Retail: AI, AR, and Digital Transformation Trends

The Future of Retail: AI, AR, and Digital Transformation Trends

Retail is evolving at an unprecedented pace, driven by AI (Artificial Intelligence), AR (Augmented Reality), and digital transformation technologies, revolutionizing the retail industry. Traditional brick-and-mortar stores are no longer just about selling products—they are becoming smart, interactiv

Read More →

Have a Project in Mind?

Let's discuss how we can help bring your ideas to life with our expertise in web and mobile development.

Start a Conversationhello@oozou.com
OOZOU Logo

Bangkok · Singapore · Hong Kong

X
Facebook
Instagram
LinkedIn

Services

  • Web Development
  • AI Agents & Generative AI
  • Mobile App Development
  • Data Analytics & Engineering
  • UI/UX & Product Design
  • Digital Transformation

Company

  • About Us
  • Careers
  • Blog
  • Whitepapers
  • Case Studies
  • Today I Learned
  • Contact

More

  • Industries
  • Get an Estimate
  • Partner Network
© 2026 OOZOU. All rights reserved.
Privacy PolicyCode of ConductABAC Policy