android R.string. retrieving value [duplicate] - android

This question already has answers here:
Accessing contents of R.string using a variable to represent the resource name
(4 answers)
Android, getting resource ID from string?
(14 answers)
How to get a resource id with a known resource name?
(10 answers)
Closed 3 years ago.
I'm practicing my Java and I'm trying to make something like simple text game. Now there are two main characters and each one have their own pop up text.
I've put the text in string.xml with their own names like "dialog1" "dialog2" "dialog3" and I want to retrieve the strings with R.string.dialog* but I want to make some function to change numbers something like "dialog" + int
How to make something like this??
I made it like if you tap on screeny the its displayed "dialog1" massage when you tap second time the other character should display next part "dialog2". Now I don't want to do it manually r.string.dialog1 then r.string.dialog2 I want to make function of this but I don't know how to pass value "dialog" + int.

You can put a parameter in the xml as explained in this post Are parameters in strings.xml possible?
You need to add %1$d wherever you want in the string resource so when you want to get the string you send the parameter.
<string name="dialog1"> This is %1$d</string>
And when you want to get it
getString(R.string.dialog1, "1")
If you want more parameters you change the parameter number
<string name="parameters"> Many parameters %1$d %2$d %3$d</string>
and then
getString(R.string.parameters, "1", "2", "3")

If you need to format your strings, then you can do so by putting your format arguments in the string resource, as demonstrated by the following example 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. Then, format the string by calling getString(int, Object...). For example:
String text = getString(R.string.welcome_messages, username, mailCount);
Quoted from: Android Documentation

If you want a method to load the values stored in strings.xml, you have to create an array of ints containing the ids of the resources.
Example:
Create messages into strings.xml:
<resources>
...
...
<string name="dialog1">My first message</string>
<string name="dialog2">My second message</string>
<string name="dialog3">My third message</string>
</resources>
Then use this method to load the values according the index of the element defined into the array:
int[] messageValues = new int[] {R.string.dialog1,R.string.dialog2,R.string.dialog3};
private String getDialogMessage(Context context, int dialogIndex){
if(dialogIndex >= messageValues.length) //Check if element exist in array
return "";
return context.getResources().getString(messageValues[dialogIndex]);
}
and these are examples of how to used the method:
System.out.println(getDialogMessage(getApplicationContext() , 0)); //get first element message.
System.out.println(getDialogMessage(getApplicationContext() , 1)); //get second element message.
System.out.println(getDialogMessage(getApplicationContext() , 2)); //get third element message.
System.out.println(getDialogMessage(getApplicationContext() , 14)); //returns "" , because the element doesnt exist into the array..

Related

Why a string resource is showing as an integer?

I am developing an app where I have in some parts of code a
textview.setText(R.string.resource1 + progress + R.string.resource2);
but when I run the app, instead of showing the strings as words (these are words translated in some languages) the app shows something like in the textview
2131755168 106(this is okay, is the variable progress) 62131755102
those numbers should be words in English
You are getting that result because you are concatenating values with mixed types, the R.string.resource1 is an int lookup value for retrieving text (it's not the text itself). When concatenating, Java will just 'toString' those int values.
You have two options...
Option 1 (Recommended):
Define a string resource that uses format arguments, then pass the progress in. It is cleaner at the call site and more flexible than option 2 below.
<string name="progress_label">Before %1$d After</string>
textView.setText(getString(R.string.progress_label, progress));
Option 2:
This is less ideal because it doesn't allow you to adapt the phrasing (if needed) for the translation.
<string name="progress_before">Before</string>
<string name="progress_after">After</string>
String before = getString(R.string.progress_before);
String after = getString(R.string.progress_after);
textView.setText(String.format("%1$s %2$d %3$s", before, progress, after));

how can I solve this error string xliff?

I have this string:
<string name="order_summary_name">Name: <xliff:g id="name" example="Thomas">%s</xliff:g></string>
using it in this format:
priceMessage=getString(R.string.order_summary_name,name);
and I keep getting an error that says: Format string 'order_summary_name' is not a valid string.
I cant find the mistake,what should I do ???
Store your styled text resource as an HTML-escaped string:
<resources>
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
</resources>
In this formatted string, a element is added. Notice that the opening bracket is HTML-escaped, using the < notation.
Then format the string as usual, but also call fromHtml(String) to convert the HTML text into styled text:
String text = getString(R.string.welcome_messages, username, mailCount);
String styledText = Html.fromHtml(text);
(or)
String styledText = Html.fromHtml(R.string.order_summary_name,name);
https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
The id attribute is just used to identify what the substitution parameter represents (in your case, it represents the quantity). It's as you said, a note, and not actually used programmatically.
That said, in Android Studio, if you have Code Folding enabled for strings, it will substitute in the ID when it shows the collapsed string. You'd see something like this:
Try this.
mTextView.setText(getString(R.string.order_summary_name, "Thoma"));

Reference one string from another string in custom xml file from resourse [duplicate]

This question already has answers here:
Reference one string from another string in strings.xml?
(11 answers)
Closed 6 years ago.
in resourse values I have one xml file, here i have facebook_app_id value:
<string name="facebook_app_id">1769573166657682</string>
I need to reuse this values in another tag
<string name="fb_login_protocol_scheme">fb1769573166657682</string>
Can I use value of facebook_app_id like this?
<string name="fb_login_protocol_scheme">fb#string/facebook_app_id</string>
What you are trying to do is concatenate a String and a String resource. I am afraid this is not possible in android through xml files.
What you can do is declare the two string and have a utility method that concatenate them.
<string name="facebook_app_id">1769573166657682</string>
<string name="fb_login_protocol_scheme">fb$1s</string>
and then just use String.format to replace the first string parameter with the app id.
I am afraid that is not possible in strings.xml.
What you can do is create the final string programmatically. Something like.
String outStr = getString(R.string.fb) +
" " + getString(R.string.facebook_app_id);

How to access values from "strings.xml" dynamically in android?

App version 2.3.3
Here is what i am looking for...
I have few strings in "values/strings.xml". I wish to retrieve the values depending on the string's "name" attribute and use it my application. The input to which string in the xml should be used comes dynamically. Here is an example...
My input from other file :: "A" => This value changes. It can be any value in A,B,C,D.
I have different strings in strings.xml, like...
<string name="A">AforApple</string>
<string name="B">BforBall</string>
<string name="C">CforCat</string>
<string name="D">DforDog</string>
Now, how do i programmatically get the value(AforApple) of the String with name="A".
String str = context.getResources().getString(R.string.A)
or u can use
textBox.setText(R.string.A);
do not forget to import the package com.yourpackackage.R.
You need to use getResources() method:
String a = getResources().getString(R.string.A);
Just for the record, you can also dynamically generate it using reflection.
int stringRes=R.string.class.getField("Here you put the dynamically generated input, such as A").getInt(null);
textView.setText(stringRes);
This will return the resource int value from string XML based on the input, as long as the input value "A" matches string name in the XML, this will retrieve them dynamically.
To access a String resource/value that you’ve defined in an XML file from your Android/Java code, use the Android getString method, like this:
String A = getString(R.string.a_string);
If your code gets a string like "A" and you are trying to dynamically find the string in your resources that matches that name, I don't think you can do that.
Instead of using the strings.xml, you might want to use arrays.xml and build a HashMap from that before you need to access those strings
Try this:
String word = getResources().getString(R.string.A);
Check out the link here.
You can use this code:
getText(R.string.A);
Basically, you need to pass the resource id as a parameter to the getText() method.

Are parameters in strings.xml possible? [duplicate]

This question already has answers here:
Is it possible to have placeholders in strings.xml for runtime values?
(14 answers)
Closed 5 years ago.
In my Android app I'am going to implement my strings with internationalization. I have a problem with the grammar and the way sentences build in different languages.
For example:
"5 minutes ago" - English
"vor 5 Minuten" - German
Can I do something like the following in strings.xml?
<string name="timeFormat">{0} minutes ago</string>
And then some magic like
getString(R.id.timeFormat, dynamicTimeValue)
This behaviour would solve the other problem with different word orders as well.
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>
If you need two variables in the XML, you can use:
%1$d text... %2$d or %1$s text... %2$s for string variables.
Example :
strings.xml
<string name="notyet">Website %1$s isn\'t yet available, I\'m working on it, please wait %2$s more days</string>
activity.java
String site = "site.tld";
String days = "11";
//Toast example
String notyet = getString(R.string.notyet, site, days);
Toast.makeText(getApplicationContext(), notyet, Toast.LENGTH_LONG).show();
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);
If you wish more look at: http://developer.android.com/intl/pt-br/guide/topics/resources/string-resource.html#FormattingAndStyling
There is many ways to use it and i recomend you to see this documentation about String Format.
http://developer.android.com/intl/pt-br/reference/java/util/Formatter.html
But, if you need only one variable, you'll need to use %[type] where [type] could be any Flag (see Flag types inside site above). (i.e. "My name is %s" or to set my name UPPERCASE, use this "My name is %S")
<string name="welcome_messages">Hello, %1$S! You have %2$d new message(s) and your quote is %3$.2f%%.</string>
Hello, ANDROID! You have 1 new message(s) and your quote is 80,50%.
Note that for this particular application there's a standard library function, android.text.format.DateUtils.getRelativeTimeSpanString().

Categories

Resources