such as:
strings.xml:
<resource>
<string name="my_title">Train Video</string>
</resources>
i use this string in Java code many cases, such as:
String title = resources.getString(R.string.my_title) // title="Train Video"
textView.setText(title)
Now i get the new string valued "Train Video Demo" from server, i want to update the strings value so that when i using the following code to get new string:
String title = resources.getString(R.string.my_title) // title="Train Video Demo"
if i can't change Java code, how can i achieve it?
i know Facebook Android App can do it dynamically, i don't know it's program. Is there some blogs to explain this?
so you want to change value dynamically for that you can add %1$s in your string file
string.xml
<resource>
<string name="my_title">Title: %1$s</string>
</resources>
Activity.kt
resources.getString(R.string.my_title, title)
Here the title is dynamic value can be from API or static list.
you can't change the values in strings.xml, so you should just start off by getting all the values you need from the server to begin with, then you can cache those by using a database, you could even use a pre-populated database when releasing your app, then read those values out of the db when you need them. to update your values, make an api call, check if there are new values available, update your local db, app continues to work as normal
You can set the value of textview directly to the string you got.
textView.setText("Train Video Demo")
Related
Everybody knows that if we have:
ekran3.setText("VAT Tax:");
We may (or even we SHOULD) convert it to:
ekran3.setText(getString(R.string.kwotaVat));
and add in strings.xml:
<string name="kwotaVat">VAT Tax:</string>
But is there some kind of trick to do it automatically? For example by clicking RMB on text and selecting some option? It would be nice to know it in fact it will save us a lot of time than while we're doing it manually.
If you are using Eclipse you may extract the string directly into the strings.xml file by placing the mouse within the string and hitting Ctrl + 1. It will bring up the dialog as followed and you may select "Extract String". You then give it a name (Ex: kwotaVat) and you're done.
hey you do not need to use getString() to convert it to string the values xml file is already having data in string form so you just need to use the following code to set the string
ekran3.setText(R.string.kwotaVat);
where ekran3 is the object of your text view
and kwotaVat is the id of your value string
for more detail od android codes have look here http://grabcodes.blogspot.com/
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.
I am facing an issue with "string.format" in android application. In my application when the user changes his language preferences from default (english) to any other foreign language (japanese,german etc) the variable string positioning is giving a force close error. Please refer the code below:
temp = String.format(locale,getResources().getString(R.string.temp_string), value, name);
where, temp_string = "The parcel number %1$d belongs to %2$s" for default selection (english)
when other languages are selected in some of them %2$s comes before %1$d . Due to which the the application force closes. Is there a way around to dynamically handle the variable strings(value,name).
I'd do something like:
temp = getResources().getString(R.string.temp_string, value, name);
As you see, getString() method can also receive parameters to format. Then, place different strings resources on different folders. For instance:
res/
values/
string.xml <--- here you put "The parcel number %1$d belongs to %2$s"
values-de/
string.xml <--- here you put "The parcel number %2$d belongs to %1$s"
I'm just giving you an example; I actually do not know how germany order is. I just want to explain what you actually have to try.
i had some items in my strings.xml file that i want to change programatically, and originally i was doing it through a setText();call but now i am attempting to translate my app to a different language which means everything needs to be set in my strings.xml file. is it possible to put all the text for my app into a strings.xml and change things programatically through references to the string names, instead of using the setText() function call? for example how would i reference "GrandTotal"?
<string name="GrandTotal">Grand Total:</string>
<string name="choose_prompt">Choose a Mode</string>
You can use setText(R.string.GrandTotal);
If you don't have the possibility to set the text via resId directly you can use getString(R.string.GrandTotal);
To avoid confusion between resourceIds and real ints, you could also use statements like
String s = getResources().getString( R.string.grand_total );
but for most ui methods an overload often provides support for passing directly resourceIds as #Keyboardsurfer said
Try this way . I hope it helps you .
setText(getResources().getString(R.string.GrandTotal));
in an Acrivity:
String str = getString(R.string.choose_prompt);
or
String str = this.getString(R.string.choose_prompt);
Is it possible to reference a string in strings.xml
Eg:
<string name="application_name">#string/first_name Browser</string>
<string name="first_name">Chrome</string>
Where depending on requirements, i can switch the value of first_name to "Chrome", "Firefox" or "Opera".
You can give the reference of string resource, but limitation are as follows
<string name="first_name">Chrome</string>
<string name="application_name">#string/first_name</string> // gives "Chrome"
<string name="application_name">Chrome #string/first_name</string> // gives "Chrome #string/first_name"
<string name="application_name">#string/first_name Chrome </string> // gives error
If content starts with "#" then Android considers this is a referenced string, see last case which gives an error because Android's tools take # and the next string to it as the string's reference name, it will try to find a resource called "#string/first_name Chrome" which doesn't exist.
You can use String Format to dynamically assign sub-strings like <string name="application_name">%1$s browser</string>
to use
String text = String.format(res.getString(R.string.application_name), "Chrome");
Yes, you can do so without having to add any Java/Kotlin code, using this small library that allows you to do so using XML only at buildtime. So for your case, you'd have to set up your strings like this:
<string name="application_name">${first_name} Browser</string>
<string name="first_name">Chrome</string>
And then after running the gradle plugin, you'll get this:
<!-- Auto generated during compilation -->
<string name="application_name">Chrome Browser</string>
This is the link to the library: https://github.com/LikeTheSalad/android-stem
Disclaimer: I'm the author of this library.
The Strings in the strings.xml are fixed and cannot be changed at run time. You will have to define a string for each case, and do the switch in the code.
String name;
if (/* browser is Chrome*/) {
name = getString(R.string.first_name_chrome);
} else if (/* browser is Firefox */) {
name = getString(R.string.first_name_firefox);
}
You can however make the application select the correct string for different languages automatically. This can be done by placing string files in localized folders (values-en, values-fr, values-pl etc).
You can read more about localization at http://www.icanlocalize.com/site/tutorials/android-application-localization-tutorial/