I'm a beginner in Android, so I hope everyone can help me to change String in strings.xml from programming.
Here is the original string:
<string name="cv_name">Hong Sengheng</string>
And I want to reset it's value to Sengheng.
<string name="cv_name">Sengheng</string>
So, hopefully everyone give me idea.
One thing what you have to understand here is that, when you provide a data as a Resource, it can't be modified during run time. For example, the drawables what you have in your drawable folder can't be modified at run time. To be precise, the "res" folder can't be modified programatically.
This applies to Strings.xml also, i.e "Values" folder. If at all you want a String which has to be modified at runtime, create a separate class and have your strings placed in this Class and access during run time.
You can not change the value of String.xml at runtime but you can change the text of that particular Ui where you pass that String...
You cann't change the value of string.xml at the runtime for this u can make a seprate class and change string value at the run time and use it
while the value of a resource should not be changed programmatically you can certainly assign a string resource to a variable String mystring = getResources().getString(R.string.mystring_res); and then change that variable's value mystring="new value";
Related
I want to use the string resources to save some key values. So, I have something like this:
<string name="key_name">key_name</string>
The resource name and the resource value are the same. There is no need for me to make a difference between them, as it is just to cleanly store one value.
To reduce redundant information,
is there a way to just tell android-studio that the name equals the value? Something like this?
<string name="key_name"/>
I think you can't do it. It looks like you create a variable, you must name it and when you need you can set its value.
You can't create a variable with same values with name at the same time.
I think you have the wrong resource type. A string resource is used for mapping a name to a text. From what you're describing here you only need the name (and then can extrapolate the text from it).
Using strings with matching name and value shouldn't be necessary:
If you're going to be referencing them from Java or Kotlin code, you still need to know the name (i.e. R.string.key_name), so you might as well just use the string value there.
If it's for using inside XML code (where you want to pass the value of a string, e.g. text="#string/key_name") most of the time you can just use the raw string there (i.e. text="key_name").
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/
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);
Can you change the values of a R.string programmatically in an android program? I need to pull some API information (for example battery state, battery percentage, android os version) and would like to save it a R.string value. I know how to read it:
String helloValue= getResources().getString(R.string.hello);
I've also looked at: Change value of R.string programically? but it seems that only involves changing language and he ended up doing it a different way. Can anyone lend a hand please? I've also looked here: http://developer.android.com/guide/topics/resources/string-resource.html and found nothing to help out either :(
You can't change strings.xml dynamically since it's a compiled resource. There are other mechanisms for saving data in Android, here's a nice post that covers this topic: Data Storage. Hope this helps.
If you need to save small amounts of String information you should be using SharedPreferences that's exactly what it's for :)
There are many ways to finish your ideas. But, you cannot change your String resource, (as well as R.java resource), or layout resource,... and any other resource. You can do this by change resource to assets. this can read and write :)
You can get value from res/string value by this way.
String str = getResources().getString(R.string.<name_of_value>);
Now you can further use this value of this(str) String variable.
But according to me there is no way to set value of res/string
can anybody help me ?? I want to add String value through coding in string.xml
I am doing this.
String name = getResources().getString(R.string.name);
if(name.lenght() < 1 ){
// getResources().setString(R.string.name);??????????????????????
}
My string.xml is
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="name"></string>
</resources>
does anybody know how i can add value of name in string.xml though coding.
Thank you!!
The resources are pretty much set in stone, so you can't modify them at runtime. If you need to store some new strings, use SharedPreferences or SQLite.
I don't think you want to do that. If you are trying to store a value in a persistent way, take a look at SharedPrefences. Google has a good introduction to it here.
It is not possible to modify the resources of an APK during runtime.
You can't edit those resources directly. You might want to look into sharedpreferences http://developer.android.com/reference/android/content/SharedPreferences.html or creating your own xml file.
You cant edit a resource or add a resource once the code is compiled. I dont know exactly what setResource does, but once your program is compiled, android builds the gen files which designate a certain amount of space for those variables, changing the variable once written would cause overflow or outofbounds errors with memory. If you want persistent values try using the SharedPrefs, SQL or even your own XML stored within the directory of the app, which you could set to only be readable by your app.