Sergio
Sergio

Reputation: 529

Define multiple buttons

Is there a way to define 31 buttons in one action.. Something like this :

    Button but[] = new Button[31];
 for(int i=1;i<32;i++)
 {
     but[i] = (Button) findViewById(R.id.Button0+i ---? );

 }

Upvotes: 0

Views: 111

Answers (2)

Gabriel Negut
Gabriel Negut

Reputation: 13960

ViewGroup parent = (ViewGroup)findViewById(R.id.PARENT_ID_HERE);
Button but[] = new Button[31];
for(int i=1;i<32;i++)
{
    but[i] = new Button(this);
    // set listeners and stuff
    parent.addView(but[i]);
}

Upvotes: 1

ameyume
ameyume

Reputation: 2460

In your ways is to define buttons in layout file. You can define buttons in java activity file by addview method to add buttos to its' parent view.

Upvotes: 0

Related Questions