• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps too many buttons

DR doggy

Newbie
i have a program i want to do which has 5 grids of 5x5 squares which need to be clickable, plus 10-20 control buttons, problem is that after about 80 onclick listeners i start to get what i think are memory errors, i was thinking about radio buttons to control my grid, but i wonder if maybe someone can advise a better way than the 125 buttons? in vb6 button arrays were easy...
 
If you can logically group your buttons into grids, perhaps you could subclass gridlayout or w/e layout you are using for the grids and then create the buttons programmaticaly. Then, the button grib becomes your unit, and you lay them out however you see fit. As for the listeners, you could make your layout subclass implement OnClickListener and override the onClick method that first determines whitch button was clicked, then reacts accordingly.
 
im not sure i understand what you are saying; are you saying to use the listener on the framelayout instead of the buttons?


right now i have 5 frame layouts, with 25 buttons each,

specifically the problem is when "i" goes a bit higher i get error:

for(int i=1; i<76; i++) {{
String buttonID = "ImageButton" + i;
matrix1 = ((ImageButton) findViewById(getResources().getIdentifier(buttonID, "id", getPackageName())));
matrix1.setOnClickListener(this);
}}
 
Instead of creating multiple separate listener for each button or control just create single listener for each type of control and assign this listener to all these controls. while implementing onClick() method of OnClickListener class check the ID of button and then take the appropriate action. here is the reference code...

Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);

button1 .setOnClickListener(buttonListener);
button2 .setOnClickListener(buttonListener);
.
.

.
.
.

.


onClickListener buttonListener = new OnClickListener() {

@Override
public void onClick(View view) {
if(view.getId() == R.id.button1 ) {
//do action intended for button 1
}
else if(view.getId() == R.id.button2 ) {
//do action intended for button 2
}

}
}

Hope this is what you want.
 
that is what i am doing, but after button 80 or so app crashes, however i found a solution using radiobuttons/groups instead, thnx
 
Back
Top Bottom