I have this code.
rele[0] = new ReleController(findViewById(R.id.button1), 1, this);
rele[1] = new ReleController(findViewById(R.id.button2), 2, this);
rele[2] = new ReleController(findViewById(R.id.button3), 3, this);
rele[3] = new ReleController(findViewById(R.id.button4), 4, this);
And my button layout looks like:
<LinearLayout style="@style/CoreLayout">
<ImageButton
android:id="@+id/button1"
style="@style/Button.Blue"
android:src="@mipmap/ic_arrow_left" />
<ImageButton
android:id="@+id/button2"
style="@style/Button.Green"
android:src="@mipmap/ic_arrow_right"/>
</LinearLayout>
<LinearLayout style="@style/CoreLayout">
<ImageButton
android:id="@+id/button3"
style="@style/Button.Yellow"
android:src="@mipmap/ic_arrow_up"/>
<ImageButton
android:id="@+id/button4"
style="@style/Button.Purple"
android:src="@mipmap/ic_arrow_down"/>
</LinearLayout>
I am trying to figure if there is a way to do something like this:
for(int i = 0; i < 4; i++)
{
rele[i] = new ReleController(findViewById(R.id.button[i+1]), i+1, this);
}
In real, I have 8 buttons, so it is really annoying to write all these lines for all buttons.
I am wondering if the button IDs can be declared as array like this:
android:id="@+id/button[1]"
android:id="@+id/button[2]"
android:id="@+id/button[3]"
...
Thank you for all your answers.
int[] = {R.id.1, R.id.2, R.id.N}