0

I have been trying to develop an Android app of a simple sound board, which will play several long sounds, and only one at a time, not simultaneously. I have the sound part playing great. I want to assign text for each button.

I have a strings.xml set up like so:

<string name="quote01">quote01</string>
<string name="quote02">quote02</string>
<string name="quote03">quote03</string>

In my main.xml, I have my buttonIds set up like this:

    final int[] buttonIds = { R.id.button01, R.id.button02, R.id.button03, 
        R.id.button04, R.id.button05, R.id.button06, 
        R.id.button07, R.id.button08, R.id.button09, 
        R.id.button10, R.id.button11, R.id.button12, 
        R.id.button13, R.id.button14, R.id.button15, 
        R.id.button16, R.id.button16, R.id.button17, 
        R.id.button18, R.id.button19, R.id.button20, 
        R.id.button21, R.id.button22, R.id.button23, 
        R.id.button24, R.id.button25 };

I could just assign the button text like this to get it to work:

Button s01 = (Button) findViewById(R.id.button01); 
s01.setText(this.getString(R.string.quote01));
Button s02 = (Button) findViewById(R.id.button02); 
s02.setText(this.getString(R.string.quote02));
Button s03 = (Button) findViewById(R.id.button03); 
s03.setText(this.getString(R.string.quote03));

But I’d rather use a loop to avoid repetitive code. I have a non-working loop like the below:

for(int i = 0; i < buttonIds.length; i++) { 
    Button soundButton = (Button)findViewById(buttonIds[i]); 
    soundButton.setText(this.getString(R.string.buttonIds[i]);
}

But in the 3rd line above, buttonIds can’t be resolved to a field. I have tried putting brackets or parantheis around the buttonIds[1] but my syntax isn’t right. I am sure this is a super easy question, but it is illuding me. Any suggestions?

Thanks in advance! Maytag87

1 Answer 1

1

See if this helps:

Resources res = getResources();
for(int i = 0; i < buttonIds.length; i++) { 
    Button soundButton = (Button)findViewById(buttonIds[i]); 
    soundButton.setText(res.getIdentifier("quote" + (i < 10 ? "0" + i : i), "string", getPackageName()));
}

Also your Strings set start from quote01 and the loop starts from i = 0, so you have to synchronize the two values.

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

5 Comments

Thanks! I cant complile with the two semicolons at the end, but if I take out the one in the middle:soundButton.setText(res.getIdentifier("quote" + (i < 10 ? "0" + i : i), "string", getPackageName())); then it compiles. Hover the app get a force close. Getting closer though, I can feel it!
@user1362308 I've put an extra ; that should have been removed. Your strings start from quote01, quote02 etc and the for loop starts from i = 0 so at the start you'll have quote00. Either modify the strings to start from quote00, quote01 etc or make the loop start from i = 1 and continue until i <= buttonsIds.length.
Yes, setting the strings file to start at quote00 did the trick, worked flawlessly! That is some cool code, thanks so much! Appreciate the help and question answered!!!
Taking forward @Luksprog's observation, you can also do it this way : soundButton.setText(res.getIdentifier("quote" + ((i+1) < 10 ? "0" + (i+1) : i+1), "string", getPackageName()));
Okay. So you settled for renaming your strings. A better approach indeed :)

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.