Alexander Kurys
Alexander Kurys

Reputation: 3

Some alternative to hundreds of buttons

Im going to write some android app, which will basically consists of two activities. So first should have a lot of buttons (100+) and on click on any of them I will just get some special id and move to second activity. But is there any alternative to declare that hundreds of buttons and copy/paste one piece of code to every of them setting almost same onClickLister? Is there any special construction? Thanks

Edit: every of buttons are actually indexed from 1 to n. And on the click second activity will be launched and get that index to show it. I cant basically use any spinner or smth else, because there will be 3 rows of clickable things and each of them carring different images

Edit 2: so, to give you an idea, im going to do some table of buttons like in Angry Birds menu when you actually choosing the level you want to play. So, on click you will get id of button and start second activity

Upvotes: 0

Views: 366

Answers (3)

Rafiq
Rafiq

Reputation: 2000

Call the method to add buttons

private void addButton(){       
    LinearLayout view = (LinearLayout) findViewById(R.id.linear_layout_id_here);            
     Button btn  = null;
     int w = 50;
     int h = 25;
    for(int i=1; i<100; i++) {
        btn = new Button(this);
        btn.setLayoutParams(new LayoutParams(w,h));
        btn.setText("button " +i);
        btn.setTag(""+i);
        btn.setOnClickListener(onClickBtn);  
        view.addView(btn);
        btn = null;
    }           
}

Call this method for handling onclick on button

    private View.OnClickListener onClickBtn = new View.OnClickListener() {
    public void onClick(View view) {
        final int tag = Integer.parseInt(view.getTag().toString());
        switch (tag) {
        case 1:
            // Do stuff
            break;
        case 2:
            // Do stuff
            break;

        default:
            break;
        }
    }
};

Upvotes: 1

Zelter Ady
Zelter Ady

Reputation: 6338

You may add buttons in code, something like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    /*your code here*/

    GroupView gw =findViewById(R.id.pnlButtonscontainer);  //find the panel to add the buttons

    for(int i=0; i<100; i++) {
        Button b = new Button(this);
        b.setLayoutParameters(new LayoutParameters(w,h));
        b.settext = i+"";
        b.setOnClickListener(new OnClickListener(){
        });
    }
}

I coded directly into browser, so some syntax error may appear in my code, but this is the point, a way, not the only one, to add 100 buttons.

Upvotes: 0

Dominik
Dominik

Reputation: 673

You should use a ListView.

ListViews are great for handling a lot of items at the same time. They are also natural for the user. Additionally, you use only one click listener - OnItemClickListener.

There's a useful example on how to work with ListViews in the Android Referenence.

Upvotes: 1

Related Questions