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