How to show integer from activity in XML?
I am using XML output in my application. So the main activity just tells android to show the XML layout of the main one.
But what if I have an integer opcode in a variable, and I want that integer variable to appear on the display as well?
How do I PUSH an integer variable in XML ??? From a basic XML reference to other strings in XML, it's easy - @ string / app_name ... but how can I use an integer variable from an activity?
a source to share
Presumably you are displaying text in TextView
.
You can display text either from a string resource (defined in XML and called R.string.*
as you mention) or from String
runtime.
You cannot change XML resources at runtime; you use them for fixed values like labels or other UI text. Thus, there is no way to "push" a value for XML.
But you can happily do something like this at runtime by dynamically updating your interface:
int userAge = calculateUsersAge();
TextView age = (TextView) findViewById(R.id.age_field);
age.setText(userAge +" years old");
Or better if there are no hardcoded values in the code:
age.setText(getString(R.string.years_old, userAge));
Where years_old
is the text "% d years" in yours res/values/strings.xml
and "% d Jahre alt" in yours res/values-de/strings.xml
, etc.
a source to share