Home > front end >  how can i change string value dynamically
how can i change string value dynamically

Time:01-19

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?

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

You can set the value of textview directly to the string you got.

textView.setText("Train Video Demo")
  •  Tags:  
  • Related