I\'ve found the R.string
pretty awesome for keeping hardcoded strings out of my code, and I\'d like to keep using it in a utility class that works with models i
Somehow didn't like the hacky solutions of storing static values so came up with a bit longer but a clean version which can be tested as well.
Found 2 possible ways to do it-
e.g.
data class MyModel(val resources: Resources) {
fun getNameString(): String {
resources.getString(R.string.someString)
}
}
Before you read: This version uses Data binding
XML-
Activity/Fragment-
val binding = NameOfYourBinding.inflate(inflater)
binding.someStringFetchedFromRes = resources.getString(R.string.someStringFetchedFromRes)
Sometimes, you need to change the text based on a field in a model. So you would data-bind that model as well and since your activity/fragment knows about the model, you can very well fetch the value and then data-bind the string based on that.