1

In my Android app I want to make a list of options that can be clicked. On whichever item you click, it always brings you to the same next screen but with a different .putextra(). I can do this with a simple ListAdapter as is done here.

That works fine, but I do want the values to be translated in the app for different languages. I guess this means I need to define all the values in the listview in my strings.xml. I can of course define some values in strings.xml, but I wouldn't know where to start to get values from my strings.xml into a listview.

Does anybody know how I would be able to get translatable values into an Android ListView? Any tips are welcome!

2 Answers 2

5

You have two options:

  1. storing strings in strings.xml and make a list or array contains their ids. you can use what @TabrejKhan has said for this option.
  2. Storing a string array as follows and pass it to your adapter.

Like:

<string-array name="my_string_list">
    <item>String 1</item>
    <item>String 2</item>
    <item>String 3</item>
    <item>String 4</item>
    <item>String 5</item>
</string-array>

And in your java code create the adapter

ArrayAdapter<CharSequence> dataAdapter = ArrayAdapter.createFromResource(activity,
                R.array.my_string_list);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your answers. Do you consider either one to be the better one? And if so, why?
I prefer second option, as you don't occupy extra space in your code and in R.java
The second, for sure!
Alright, so the second option it is. But where do I store this string array? In the fragment layout file, or somewhere else?
in same file strings.xml
0

A simple answer is.... Create your array like this:

String[] listItems = {R.string.text1, R.string.text2, R.string.text3, R.string.text4 /*and so on*/ }; 

1 Comment

But, "R.string.text1" ..... are "int" items, how can you store them in a String Array. Can you explain?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.