How to get all Buttons ID's in one time on Android

Let's say you named your button button_0, button_1, .. button_15. You can do:

for (int i = 0; i < 16; i++) {
    int id = getResources().getIdentifier("button_"+i, "id", getPackageName());
    button[i] = (Button) findViewById(id);
}

Well, if all 16 of those buttons are inside one view or layout, then you could do the following.

ArrayList<View> allButtons; 
allButtons = ((LinearLayout) findViewById(R.id.button_container)).getTouchables();

This assumes that your container (in this example a LinearLayout) contains no Touchable that is not a Button.


  1. use Butterknife view injections library
  2. Download Android ButterKnife Zelezny plugin for Android Studio or Intellij IDEA and initialize all your views from current layout by 1 click