Scenario: My Activity contains a few buttons with ID: btn1, btn2 and so on. I want the code to access these buttons through loop counter. So, which of the below two approaches is more efficient? Thanks!
getChildAt():
for (int optionCounter = 0; optionCounter < bigNumber; optionCounter++) {
optionButton = (Button) buttonRelativeLayout.getChildAt(optionCounter); //Layout contains Buttons
optionButton.setText("some text"); }
NOTE: buttonRelativeLayout was just introduced artificially to access Buttons by child number. Which otherwise doesn't serve any purpose.
getIdentifier():
for (int optionCounter = 0; optionCounter < bigNumber; optionCounter++) {
String buttonID = "btn" + optionCounter;
int resID = getResources().getIdentifier(buttonID, "id", "com.sample.project");
optionButton = ((Button) findViewById(resID));
optionButton.setText("some text"); }
Note: use of getIdentifier function is discouraged. It is much more efficient to retrieve resources by identifier than by name -Developers Reference. And the reference document stops there.
Thanks again!