Skip to content Skip to sidebar Skip to footer

How To Show Integer From Activity In Xml?

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 a

Solution 1:

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 a String at 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:

intuserAge= calculateUsersAge();
TextViewage= (TextView) findViewById(R.id.age_field);
age.setText(userAge +" years old");

Or better, ensuring there are no hardcoded values in the code:

age.setText(getString(R.string.years_old, userAge));

Where years_old is the text "%d years old" in your res/values/strings.xml and "%d Jahre alt" in your res/values-de/strings.xml, and so on.

Solution 2:

int app_Integer = 10;

Convert from int to String to set in your textview::

Stringapp_String="" +app_Integer;

Post a Comment for "How To Show Integer From Activity In Xml?"