2

I have a working application but want to optimise the code. Below is one example where I create 10 separate imagebuttons (note the incrementing objectname and XML reference for each) and set their listeners. Can anyone suggest a more optimal way of doing this, perhaps in a dynamic method/loop please? Thanks....

private void initialiseButtons() {  
    ImageButton imageButton1 = (ImageButton)this.findViewById(R.id.imageButton1);
    imageButton1.setOnClickListener(this);
    ImageButton imageButton2 = (ImageButton)this.findViewById(R.id.imageButton2);
    imageButton2.setOnClickListener(this);
    ImageButton imageButton3 = (ImageButton)this.findViewById(R.id.imageButton3);
    imageButton3.setOnClickListener(this);
    ImageButton imageButton4 = (ImageButton)this.findViewById(R.id.imageButton4);
    imageButton4.setOnClickListener(this);
    ImageButton imageButton5 = (ImageButton)this.findViewById(R.id.imageButton5);
    imageButton5.setOnClickListener(this);
    ImageButton imageButton6 = (ImageButton)this.findViewById(R.id.imageButton6);
    imageButton6.setOnClickListener(this);
    ImageButton imageButton7 = (ImageButton)this.findViewById(R.id.imageButton7);
    imageButton7.setOnClickListener(this);
    ImageButton imageButton8 = (ImageButton)this.findViewById(R.id.imageButton8);
    imageButton8.setOnClickListener(this);
    ImageButton imageButton9 = (ImageButton)this.findViewById(R.id.imageButton9);
    imageButton9.setOnClickListener(this);
    ImageButton imageButton0 = (ImageButton)this.findViewById(R.id.imageButton0);
    imageButton0.setOnClickListener(this);
}

5 Answers 5

1

You could use a loop and use getIdentifier() method.

int idToInitialize;
ImageButton ib;
for (int i = 0; i < 9; i++) {
    idToInitialize = getResources().getIdentifier("imageButton" + i, "id", getPackageName());
    ib = (ImageButton) this.findViewById(idToInitialize);
    ib.setOnClickListener(this);
}

Hovewer, it is very slow if we compare to the normal method.

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

Comments

1

You can reduce the boilerplate code by using http://androidannotations.org/ which will allow you to do someting like that

 @ViewById
 ImageButton imageButton1;

but it would be perhaps better to use an array or a list of buttons rather than multiple references, something like that for example :

private void init() {
    int[] ids=new int[]{R.id.imageButton1, R.id.imageButton2 ...};
    List<ImageButton> buttons=new ArrayList<ImageButton>;
    for(int id : ids) {
        buttons.add((ImageButton)this.findViewById(id));
    }
}

you can then easily iterate on the List, for example to set the listener

for(ImageButton button : buttons) {
   button.setOnClickListener(this);
}

Comments

0

You can use arrays, lists ...

For example:

private static final int[] BUTTONS_IDS = new int[] {R.id.imageButton0, R.id.imageButton1, R.id.imageButton2, R.id.imageButton3, R.id.imageButton4, R.id.imageButton5, R.id.imageButton6, R.id.imageButton7, R.id.imageButton8, R.id.imageButton9};

ImageButton[] buttons = new ImageButton[BUTTON_IDS.length];

private void initialiseButtons() {
    for (int i = 0; i < BUTTONS_IDS.length; i++) {
        buttons[i] = (ImageButton) findViewById(BUTTONS_IDS[i]);
        buttons[i].setOnClickListener(this);
    }
}

You can also put the list of ids in the arrays.xml file.

1 Comment

Thank you, I've implemented this and it works perfectly. Just one correction in case anyone else copy/pastes - change BUTTON_IDS to BUTTONS_IDS in the first two lines for a consistent variable name throughout. That said, it's still a top response njzk2.
0

I can't try it cause I have no Android Environmetn here. But I think this should work:

private void initialiseButtons() {
   for(int i = 0; i < 10; i++) {
      ImageButton imageButton = (ImageButton)this.findViewById(R.id.getClass().
         getField("imageButton" + i).get(R.id));
      imageButton.setOnClickListener(this);
   }
}

1 Comment

rather than R.id.getClass(), there is getResources().getIdentifier(String, String, String) which is cleaner
0

If it's only for setting the onClickListener, you can get rid off your whole initialiseButtons() methods and simply add android:onClick="handleButtonClick" in your layout xml and define that method in your activity like so:

public void handleButtonClick(View view) {
    switch(view.getId()) {
    case R.id.imageButton1: // handle button 1 pressed ....
    case R.id.imageButton2: // handle button 2 pressed ....
    // ... handle further buttons
    }
}

Comments

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.