How to call strings from strings.xml into arraylist
I am using ArrayList
and all strings are hardcoded in the adapter, but now I need multi-language support for my application. I think the best way to do this is to move all my hardcoded strings into strings.xml file and then render all the strings files I need for different languages.
I've already translated the hardcoded strings into my strings.xml file, but now I'm not sure how to call them in ArrayList
instead of having the hardcoded string there.
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
List<AdapterData> mItems;
public Adapter() {
super();
mItems = new ArrayList<>();
AdapterData data = new AdapterData();
data.setQuestion("How Old Are You?");
data.setAnswer("I Am 21 Years Old");
mItems.add(data);
data = new AdapterData();
data.setQuestion("Where Were You Born?");
data.setAnswer("I Was Born In The USA");
mItems.add(data);
data = new AdapterData();
data.setQuestion("Are You Male or Female?");
data.setAnswer("I Am Male");
mItems.add(data);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.recycler_view_card_item, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
AdapterData data = mItems.get(i);
viewHolder.mName.setText(data.getQuestion());
viewHolder.mNameTwo.setText(data.getAnswer());
}
@Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView mQuestion;
public TextView mAnswer;
public ViewHolder(View itemView) {
super(itemView);
mQuestion = (TextView)itemView.findViewById(R.id.layoutQuestion);
mAnswer = (TextView)itemView.findViewById(R.id.layoutAnswer);
}
}
}
source to share
To provide an alternative, you can also use a string array
<string name="how_old_are_you">How old are you?</string>
<string name="are_you_male_or_female">Are you male or female?</string>
...
<string name="i_am_21">I am 21 years old</string>
<string name="i_was_born_in_the_usa">I was born in the USA</string>
...
<string-array name="questions">
<item>@string/how_old_are_you</item>
<item>@string/are_you_male_or_female</item>
...
</string-array>
<string-array name="answers">
<item>@string/i_am_21</item>
<item>@string/i_was_born_in_the_usa</item>
...
</string-array>
To use a string array as a list,
List<String> questions = Arrays.asList(getResources().getStringArray(R.array.questions));
This might come in handy.
source to share
If you want an array of strings, I think a string array is the best choice. Create an XML file called arrays.xml and put below code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="array_name">
<item>String 1</item>
<item>String 2</item>
<item>String 3</item>
<item>String 4</item>
<item>String 5</item>
</string-array>
</resources>
Then get the string arrays:
String[] myStrings = getResources().getStringArray(R.array.array_name);
source to share
what you can do is
write below line in string.xml file
<string name="How_Old_Are_You">How Old Are You?</string>
and you have an arraylist
data.setQuestion(""+getResources().getString(R.string.How_Old_Are_You));
for reference see below link
how to read value from string.xml in android?
another possible solution might be if you create string array in string.xml file which will be easier
string-array name="tabs_names">
<item>My Tab 1</item>
<item>My Tab 2</item>
</string-array>
and in your action write below lines to access the array.
String[] tab_names = getResources().getStringArray(R.array.tab_names);
String tabname1=tab_names[0];//"My Tab 1"
source to share
You can get these lines using
String stringOne= getResources().getString(R.string.stringOne);
So using ArrayList you can do the following:
yourArrayList.add(stringOne)
To get them all at once, you need to name these lines in the xml file in some template.
how
string_0
string_1
string_2
.
.
So your strings.xml file looks like
<resources>
<string name="string_0">First String</string>
<string name="string_1">Second String</string>
<string name="string_2">Third String</string>
</resources>
And then you can use
for (int i = 0; i < numberOfStringsInStringsXML; i++){
int id = getResources().getIdentifier("string_"+i, string, getPackage());
String string= getResources().getString(id);
yourArrayList.add(string);
}
Finally, you will get all the lines in yourArrayList .
Then you can use ArrayList to set your data for the adapter.
source to share
Make resource array of strings:
<resources>
<string-array name="questions">
<item>@string/question1</item>
<item>@string/question2</item>
...
</string-array>
</resources>
Make localized versions of your questions. Then in your action code you can use
String[] questions = getResources().getStringArray(R.array.questions);
You can use String [] directly, or if you need it like using a list Arrays.asList(questions);
source to share