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
Author
18 person written this
sandeepsatpute9271
open
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
- Hardcode a string in code but this will not help in case of reusability and string localisation
- Use the context of an activity but in mvvm its bad practice to use context direct in view model
- 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)
}
}
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)
}
...
}
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
trimMiddle() - the missing String trim method
read article
Interpolação, Verbatim String Literals, Múltiplas Linhas – Tudo Sobre Strings no C#
read article
Leetcode — 3110. Score of a String
read article
Tipos Básicos em Kotlin - String Literals
read article
Tipos Básicos em Kotlin - Strings
read article
Quick Zig and C String Conversion Conundrums
read article
Code Smell 261 - DigiCert Underscores
read article
JavaScript: Strings, String Methods, and String Properties
read article
JavaScript: String Template Literals
read article
Top 7 PHP Strings Interview Questions and Answers
read article
Strings in java language
read article
Strings in java language
read article
Unraveling the Power of Strings in Python
read article
What to use instead of `@ember/string`
read article
JS Challenge: Check if a string is a palindrome
read article
5 Interesting Things About Strings in Java
read article
Efficient String Splitting in Go: A Solution for Gophers Dealing with API Responses
read article
Javascript : 12 méthodes essentielles pour les chaînes de caractères
read article
anagram program in java
read article
How to convert String to Integer in java
read article
JS Blog - String Manipulation
read article
Find the Index of the First Occurrence in a String (Naive and KMP Solutions)
read article
In search of subsequence
read article
383. Ransom Note
read article
Split and Join: The Dichotomy of Strings and Arrays
read article
How do Bytes live in Solidity and coexist with Strings?
read article
"str and String in Rust confuses a lot of people in the beginning"
read article
W3Schools Strings Method With Example
read article
Javascript Tagalog - Strings
read article
Resources(R.string) in viewModel in Android
currently reading
Featured ones: