0

I have a button that when clicked, the text inside changes. I'm trying to accomplish this with an array in my strings.xml, thinking that the onClick will do a for loop through the string-array

<string-array name="RG_answers">
     <item>text 1</item><!--Of course this is example text-->
     <item>text 2</item>
     <item>text 3</item>
</string-array>

I don't know if I'm just not Googling correctly, or what. Inside my onCreate() my button is basic

 mTrueButton = (Button) findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

So basic that it's only helpful (if that) by providing the names I called it. Is it possible/feasible? From what I've found I haven't seen anyone ask a question like this.

5
  • I feel like this is sarcastic. What can I do to provide more clarity for you? I will edit the OP accordingly. Commented Mar 29, 2017 at 19:28
  • Do you mean you want to cycle the text on each button click and go back to the start of the array when it reaches the end? Commented Mar 29, 2017 at 19:29
  • Yes that is correct. So when it first launches the text in the button would say "text 1", when clicked would change to "text 2" etc etc, than when it hits the end of the array, loops back to "text 1". Commented Mar 29, 2017 at 19:31
  • you need to set all the text one by one to the button when you click it just one time? Commented Mar 29, 2017 at 19:32
  • That was the plan. The thing is, I'm not entirely sure this is the right/best way to do this. I hardly doubt it is, I'm still very new to java/android development. Commented Mar 29, 2017 at 19:34

3 Answers 3

1

Is it possible to loop through the string-array?

Yes, change the button text using button.setText(text); Keep a counter as global variable so that you can cycle through the list of 'button text' you have mentioned.

eg: button.setText(array[index]);

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

Comments

0

You need three steps:

//1 Declare an index outside onClick:
int currentIndex=0;

//2- Read the answers:
Resources res = getResources();
myStrings = res.getStringArray(R.array.RG_answers);

//3- iterate thru them inside onClick:
mTrueButton.setText(myStrings[(currentIndex++)%(myStrings.length)]);

1 Comment

I think I'm missing something because declaring myStrings is throwing at my "cannot resolve symbol" when I declare it inside the onCreate(). I declared String[] myStrings; outside of the onCreate() and it doesn't display any syntax errors. However, this causes the app to instantly crash when the instance is loaded.
0

Something like this.

private int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final String[] btnText = getResources().getStringArray(R.array.RG_answers);
    mTrueButton = (Button) findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mTrueButton.setText(btnText[count]);
            if (count < btnText.length - 1) {
                count++;
            } else {
                count = 0;
            }
        }
    });
}

1 Comment

Went off and a friend of mine ended up helping me with getting the solution. Your answer is very similar to what I got working, as he and I ended up going the route of mTrueButton.setText(RG_arr[i].toString()); inside the onClick(). I'm not certain whether to mark yours as the answer, or post my own, as I can see yours working just as well as our solution.

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.