2

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.

2 Answers 2

3

You are looking for a string-array and you can access theresources inside your onCreate or afterwards.

XML file saved at res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>
</resources>

This application code retrieves a string array:

Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array)

source: https://developer.android.com/guide/topics/resources/string-resource.html

Sign up to request clarification or add additional context in comments.

Comments

0

You can just do :

public class DisplayMenuActivity extends AppCompatActivity {

Button nextButton;
TextView word;

public int[] list = {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]);
        }
    });
}}

This works because TextView.setText() can take an int as a parameter as long it's a string resource.

2 Comments

Thanks a lot for the help. This code worked but do you have any idea why my previous code didn't work? Still don't understand why the getString(R.string.word_1) didn't work. I didn't produce an error in the editor but it did crash the app on my phone.
Your code crash because you are calling the method getString() at class initialization (ie, at instance creation). When the Androdi system decides to create your Activity, it will probably miss some context to retrieve the resources. In Activity, you should never put code in field initialization or constructor, this should be done inside onCreate() method.

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.