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
.
- use Butterknife view injections library
- Download Android ButterKnife Zelezny plugin for
Android Studio
orIntellij IDEA
and initialize all your views from current layout by 1 click