0

In my activity_main.xml, I have 26 buttons (one for each letter of the alphabet). They are labeled btn_a, btn_b... btn_z.

These buttons will play a sound file on click (the pronunciation of their letter). Therefore, I want to grab a reference to their button to add the onClickListener.

This would be troublesome to do 26 times especially if I adapt this to handle other languages. So I want to do something like

Button alphaButton=new Button[26];
char[] alphabet=['a','b'... 'z'];
for(int i=0;i<26;i++){
    alphaButton[i]=(button) findViewById(R.id.btn_+alphavet[i]);
    alphaButton[i].setOnClickListener(listener);
}

I know this might work with JavaScript (given some tweaks) but I can't think of a way to programmatically grab id's in Android Studio (Java not Kotlin).

Display name
  • 413
  • 3
  • 14
  • 2
    have you tried with [this](https://stackoverflow.com/a/4865350/3789527)? – LS_ Feb 10 '22 at 13:59
  • I'll work on adding that. It looks like it's exactly what I wanted. If you can add this as an answer instead of a comment, I can mark it as correct. – Display name Feb 11 '22 at 00:20
  • @BrianTompsett-汤莱恩 Yes, that seems to be the same link that LS_ shared and it worked well. If you could add that as an answer I can mark it as correct and close this question. – Display name Feb 12 '22 at 02:33

0 Answers0