I use XML output in my app. So basically the main activity just tells the android to show the XML layout of main.
But what if I have in the activity code defined integer variable and I want this integer variable also be shown on the display?
How do I PUSH the integer variable to the XML??? From main XML reference to other strings in XML is easy – @string/app_name … but how do I use the integer variable from the activity?
Presumably you’re showing some text in a
TextView.You can display text either from a string resource (defined in XML and referred to as
R.string.*, as you mention) or from aStringat runtime.You can’t change the XML resources at runtime; you use them for fixed values like labels or other UI text. So there’s no way to “push” a value to XML.
But you can happily do something like this at runtime, dynamically updating your UI:
Or better, ensuring there are no hardcoded values in the code:
Where
years_oldis the text “%d years old” in yourres/values/strings.xmland “%d Jahre alt” in yourres/values-de/strings.xml, and so on.