Are parameters in strings.xml possible?
Yes, just format your strings in the standard String.format()
way.
See the method Context.getString(int, Object...)
and the Android or Java Formatter
documentation.
In your case, the string definition would be:
<string name="timeFormat">%1$d minutes ago</string>
Is it possible to have placeholders in strings.xml for runtime values?
Formatting and Styling
Yes, see the following from String Resources: Formatting and Styling
If you need to format your strings using
String.format(String, Object...)
, then you can do so by putting your format arguments in the string resource. For example, with the following resource:<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments:
%1$s
is a string and%2$d
is a decimal number. You can format the string with arguments from your application like this:Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
Basic Usage
Note that getString
has an overload that uses the string as a format string:
String text = res.getString(R.string.welcome_messages, username, mailCount);
Plurals
If you need to handle plurals, use this:
<plurals name="welcome_messages">
<item quantity="one">Hello, %1$s! You have a new message.</item>
<item quantity="other">Hello, %1$s! You have %2$d new messages.</item>
</plurals>
The first mailCount
param is used to decide which format to use (single or plural), the other params are your substitutions:
Resources res = getResources();
String text = res.getQuantityString(R.plurals.welcome_messages, mailCount, username, mailCount);
See String Resources: Plurals for more details.
Call variables in string.xml
In your xml file string.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="correct">Good job! The answer was %1$s\n
You made %2$s guesses.\n
Restart the app to try again.</string>
</resources>
If you want to put an other value in the text, you increment %2$s -> %3$s , etc ...
Then you can use it like in java like that :
Toast.makeText(MainActivity.this, getString(R.string.correct, comp, guesses),
Toast.LENGTH_LONG).show();
Use Strings with placeholder in XML layout android
You can use something like this.
Write your string in your (strings.xml) with declaring a string variable (%1$s) inside it. For decimal, we use (%2$d).
<string name="my_string">My string name is %1$s</string>
And inside the android code (yourFile.java), use this string where you want it.
String.format(getResources().getString(R.string.my_string), stringName);
This is not a good answer but it may help you get some idea to get going.
Thanks.
Related Topics
How to Check the Multiple Permission at Single Request in Android M
Android Gridview Row Dividers/Separators
How to Display More Than 3- Levels of Expandable List View
Play Sound Using Soundpool Example
Proguard Ignores Config File of Library
How to Get Number of Lines of Textview
Get Image from the Gallery and Show in Imageview
Highlight Listview Selected Row
How to Create a Database in Android
How to Scroll to Bottom in a Scrollview on Activity Startup
How to Check If Facebook Is Installed Android
Livedata Prevent Receive the Last Value When Start Observing
Httpurlconnection Worked Fine in Android 2.X But Not in 4.1: No Authentication Challenges Found
Error: Configuration with Name 'Default' Not Found in Android Studio
Sharedpreferences and Thread Safety
Why Does It Take So Long for Android's Mediaplayer to Prepare Some Live Streams for Playback