Does anyone know to can I translate a toast message in android studio? I don't know how to convert the string message into the resource file (String.xml) like a textview in android studio
String toastMessage1 = "Good Job! ";
String toastMessage2 = "Correct Answers";
if (dNumber >= 90) {
Toast.makeText(
MainActivity.this,
toastMessage1 + String.valueOf(dNumber) + " % " + toastMessage2,
Toast.LENGTH_LONG
).show();
}
You really should do your research first - it's quite simple, really. Read through String Resources guide - it has all the information you need. In a nutshell, what you need is to add your text into the XML file as usual:
<string name="good_job">Good Job! %1$d %% Correct Answers</string>
Then you just use getResources
and getString
:
String toastMessage = MainActivity.this.getResources().getString(R.string.good_job, dNumber);
Toast.makeText(MainActivity.this, toastMessage, Toast.LENGTH_LONG).show();
Do note that you need to have double-%, as the % char itself has special meaning inside the string and needs to be escaped.
Add a string in your strings.xml
<string name="string_name">Good Job! %d Correct Answers</string>
Use String functions for updating string.
String.format(res.getString(R.string.string_name), dNumber);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.