Logo

dev-resources.site

for different kinds of informations.

Resources(R.string) in viewModel in Android

Published at
8/5/2022
Categories
android
codequality
mvvm
strings
Author
sandeepsatpute9271
Categories
4 categories in total
android
open
codequality
open
mvvm
open
strings
open
Author
18 person written this
sandeepsatpute9271
open
Resources(R.string) in viewModel in Android

I would like to share a technique to access the strings in view model that works well.
So there are multiple ways to access the string in view model but they have there own advantages and disadvantages.
We have options like

  1. Hardcode a string in code but this will not help in case of reusability and string localisation
  2. Use the context of an activity but in mvvm its bad practice to use context direct in view model
  3. Return the string resource id which is integer and in activity get the actual string but this will increase the efforts

Let's move toward the solution
In MVVM and hilt will have addition benefits to overcome above issue.

We will add the StringResourcesProvider.kt

@Singleton
class StringResourcesProvider @Inject constructor(
    @ApplicationContext private val context: Context
) {
    fun getString(@StringRes stringResId: Int): String {
        return context.getString(stringResId)
    }
}
Enter fullscreen mode Exit fullscreen mode

Define the view model to get the string

@HiltViewModel
class AuthViewModel @Inject constructor(
    private val stringResourcesProvider: StringResourcesProvider
) : ViewModel() {
    ...
    fun validate() {
        val username: String = stringResourcesProvider.getString(R.string.username)
    }
    ...
}
Enter fullscreen mode Exit fullscreen mode

In this way we can access the string in view model or any other places.

Thanks for reading my post.
Happy codding

strings Article's
30 articles in total
Favicon
trimMiddle() - the missing String trim method
Favicon
Interpolação, Verbatim String Literals, Múltiplas Linhas – Tudo Sobre Strings no C#
Favicon
Leetcode — 3110. Score of a String
Favicon
Tipos Básicos em Kotlin - String Literals
Favicon
Tipos Básicos em Kotlin - Strings
Favicon
Quick Zig and C String Conversion Conundrums
Favicon
Code Smell 261 - DigiCert Underscores
Favicon
JavaScript: Strings, String Methods, and String Properties
Favicon
JavaScript: String Template Literals
Favicon
Top 7 PHP Strings Interview Questions and Answers
Favicon
Strings in java language
Favicon
Strings in java language
Favicon
Unraveling the Power of Strings in Python
Favicon
What to use instead of `@ember/string`
Favicon
JS Challenge: Check if a string is a palindrome
Favicon
5 Interesting Things About Strings in Java
Favicon
Efficient String Splitting in Go: A Solution for Gophers Dealing with API Responses
Favicon
Javascript : 12 méthodes essentielles pour les chaînes de caractères
Favicon
anagram program in java
Favicon
How to convert String to Integer in java
Favicon
JS Blog - String Manipulation
Favicon
Find the Index of the First Occurrence in a String (Naive and KMP Solutions)
Favicon
In search of subsequence
Favicon
383. Ransom Note
Favicon
Split and Join: The Dichotomy of Strings and Arrays
Favicon
How do Bytes live in Solidity and coexist with Strings?
Favicon
"str and String in Rust confuses a lot of people in the beginning"
Favicon
W3Schools Strings Method With Example
Favicon
Javascript Tagalog - Strings
Favicon
Resources(R.string) in viewModel in Android

Featured ones: