I have an activity that I start. It has an array that I want to contain strings from my strings.xml. Furthermore, I had a button that when clicked changes the text of my TextView to the next element in the array. So I want a changing text in the TextView after every click on the button. The text comes from the array. However I cannot get the array to get strings from strings.xml with the code:
getString(R.string.word_1)
My strings.xml looks like this:
<resources>
<string name="app_name">Name</string>
<string name="word_1">First</string>
</resources>
The activity where this Array is in has the following code:
public class DisplayMenuActivity extends AppCompatActivity {
Button nextButton;
TextView word;
public String list[] = {getString(R.string.word_1)};
private int listNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_menu);
//Word
word = (TextView) findViewById(R.id.word);
word.setText(list[0]);
//Button
nextButton = (Button) findViewById(R.id.nextButton);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listNumber++;
if (listNumber >= list.length) {
listNumber = 0;
}
word.setText(list[listNumber]);
}
});
}}
It does work when I replace getString(R.string.word_1) with "First" for example but I want to call the words from the XML file and not hard code them all. So the problem is in calling the String from XML and I've looked all over the internet for solutions but they all don't work. Anyone have an idea what I am doing wrong? Thanks for taking a look for who is looking. I appreciate it.