abolixes Ghaemy abolixes Ghaemy - 1 year ago 73
Android Question

many same code for button (Short Then)

I have this code on the Android Eclipse and I need to short the codes to decrease file size I think can short codes with for method but I'm amateur:

Button P1 = (Button) findViewById(R.id.Part1);
Button P2 = (Button) findViewById(R.id.Part2);
Button P3 = (Button) findViewById(R.id.Part3);
Button P4 = (Button) findViewById(R.id.Part4);
Button P5 = (Button) findViewById(R.id.Part5);
Button P6 = (Button) findViewById(R.id.Part6);
Button P7 = (Button) findViewById(R.id.Part7);
Button P8 = (Button) findViewById(R.id.Part8);
Button P9 = (Button) findViewById(R.id.Part9);
Button P10 = (Button) findViewById(R.id.Part10);
Button P11 = (Button) findViewById(R.id.Part11);
Button P12 = (Button) findViewById(R.id.Part12);
Button P13 = (Button) findViewById(R.id.Part13);
Button P14 = (Button) findViewById(R.id.Part14);
Button P15 = (Button) findViewById(R.id.Part15);
Button P16 = (Button) findViewById(R.id.Part16);
Button P17 = (Button) findViewById(R.id.Part17);
Button P18 = (Button) findViewById(R.id.Part18);
Button P19 = (Button) findViewById(R.id.Part19);
Button P20 = (Button) findViewById(R.id.Part20);
Button P21 = (Button) findViewById(R.id.Part21);
Button P22 = (Button) findViewById(R.id.Part22);
Button P23 = (Button) findViewById(R.id.Part23);
Button P24 = (Button) findViewById(R.id.Part24);
Button P25 = (Button) findViewById(R.id.Part25);
Button P26 = (Button) findViewById(R.id.Part26);
Button P27 = (Button) findViewById(R.id.Part27);
Button P28 = (Button) findViewById(R.id.Part28);
Button P29 = (Button) findViewById(R.id.Part29);
Button P30 = (Button) findViewById(R.id.Part30);
Button P31 = (Button) findViewById(R.id.Part31);
Button P32 = (Button) findViewById(R.id.Part32);
Button P33 = (Button) findViewById(R.id.Part33);
Button P34 = (Button) findViewById(R.id.Part34);
Button P35 = (Button) findViewById(R.id.Part35);
Button P36 = (Button) findViewById(R.id.Part36);
Button P37 = (Button) findViewById(R.id.Part37);
Button P38 = (Button) findViewById(R.id.Part38);
Button P39 = (Button) findViewById(R.id.Part39);
Button P40 = (Button) findViewById(R.id.Part40);
Button P41 = (Button) findViewById(R.id.Part41);
Button P42 = (Button) findViewById(R.id.Part42);
Button P43 = (Button) findViewById(R.id.Part43);
Button P44 = (Button) findViewById(R.id.Part44);
Button P45 = (Button) findViewById(R.id.Part45);
Button P46 = (Button) findViewById(R.id.Part46);
Button P47 = (Button) findViewById(R.id.Part47);
Button P48 = (Button) findViewById(R.id.Part48);
Button P49 = (Button) findViewById(R.id.Part49);
Button P50 = (Button) findViewById(R.id.Part50);
Button P51 = (Button) findViewById(R.id.Part51);
Button P52 = (Button) findViewById(R.id.Part52);
Button P53 = (Button) findViewById(R.id.Part53);
Button P54 = (Button) findViewById(R.id.Part54);
Button P55 = (Button) findViewById(R.id.Part55);
Button P56 = (Button) findViewById(R.id.Part56);
Button P57 = (Button) findViewById(R.id.Part57);
Button P58 = (Button) findViewById(R.id.Part58);
Button P59 = (Button) findViewById(R.id.Part59);
Button P60 = (Button) findViewById(R.id.Part60);
Button P61 = (Button) findViewById(R.id.Part61);
Button P62 = (Button) findViewById(R.id.Part62);
Button P63 = (Button) findViewById(R.id.Part63);
Button P64 = (Button) findViewById(R.id.Part64);
Button P65 = (Button) findViewById(R.id.Part65);
Button P66 = (Button) findViewById(R.id.Part66);
Button P67 = (Button) findViewById(R.id.Part67);
Button P68 = (Button) findViewById(R.id.Part68);
Button P69 = (Button) findViewById(R.id.Part69);
Button P70 = (Button) findViewById(R.id.Part70);
Button P71 = (Button) findViewById(R.id.Part71);
Button P72 = (Button) findViewById(R.id.Part72);
Button P73 = (Button) findViewById(R.id.Part73);
Button P74 = (Button) findViewById(R.id.Part74);
Button P75 = (Button) findViewById(R.id.Part75);
Button P76 = (Button) findViewById(R.id.Part76);
Button P77 = (Button) findViewById(R.id.Part77);
Button P78 = (Button) findViewById(R.id.Part78);
Button P79 = (Button) findViewById(R.id.Part79);
Button P80 = (Button) findViewById(R.id.Part80);
Button P81 = (Button) findViewById(R.id.Part81);
Button P82 = (Button) findViewById(R.id.Part82);
Button P83 = (Button) findViewById(R.id.Part83);
Button P84 = (Button) findViewById(R.id.Part84);
Button P85 = (Button) findViewById(R.id.Part85);
Button P86 = (Button) findViewById(R.id.Part86);
Button P87 = (Button) findViewById(R.id.Part87);
Button P88 = (Button) findViewById(R.id.Part88);
Button P89 = (Button) findViewById(R.id.Part89);

Thanks very long I need a code to short the codes on top.

Answer Source

if the parent layout contain only those button view you can do

for(int i = 0; i < layout.getChildCount(); i++) {
                        Button button = (Button)layout.getChildAt(i);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download