6

I managed to create buttons in a for loop and saw no reason why not to declare my varibles inside it too. Unfortunately eclipse only identifies the "bt" and doesn't want to replace my [i] with the number it represents in the loop and as a result find the correct id in my layout. Any thoughts on how to make this work? I'm also greatful for any other solution as beatiful as mine, which doesn't work ;)

Button [] bt = new Button[6];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_layout);

    bt[0] = (Button) findViewById(R.id.bt0);
    bt[1] = (Button) findViewById(R.id.bt1);//This is what i'm trying to replace
    bt[2] = (Button) findViewById(R.id.bt2);
    bt[3] = (Button) findViewById(R.id.bt3);
    bt[4] = (Button) findViewById(R.id.bt4);
    bt[5] = (Button) findViewById(R.id.bt5);


    for (int i=0; i<6; i++) {
        final int b = i;
        bt [i] = (Button)findViewById(R.id.bt[i]);    <----//Whith this
        bt [i].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent myIntent = new Intent(Start.this, MainActivity.class);
                myIntent.putExtra("players", b);
                startActivity(myIntent);

                //startActivity(new Intent(Start.this, MainActivity.class));
            }
        });

    }
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
Epcilon
  • 95
  • 1
  • 1
  • 6

2 Answers2

12

I would do the following:

private static final int[] idArray = {R.id.bt0, R.id.bt1, R.id.bt2, R.id.bt3, R.id.bt4, R.id.bt5};

private Button[] bt = new Button[idArray.length];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_layout);

    for (int i=0; i<idArray.length; i++) {
        final int b = i;
        bt [b] = (Button)findViewById(idArray[b]); // Fetch the view id from array
        bt [b].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent myIntent = new Intent(Start.this, MainActivity.class);
                myIntent.putExtra("players", b);
                startActivity(myIntent);

                //startActivity(new Intent(Start.this, MainActivity.class));
            }
        });

    }
}

If you want to add or remove buttons, just add it to idArray and all other things are dynamic already.

Lawrence Choy
  • 6,088
  • 1
  • 20
  • 30
  • Simple enough to understand and indeed beautiful. I'll give it a try as soon as I start my comp. tomorrow! – Epcilon Dec 19 '12 at 19:05
0

I think if you have group of similar buttons - they all placed inside 1 parent on layout (LinearLayout or RelativeLayout or something else). You can take get parent and retrieve all children. This way you don't need to specify id for each button.

ViewGroup buttonsView = (ViewGroup) findViewById(R.id.buttons);
List<Button> buttons = new ArrayList<Button>();
for (int i = 0; i < buttonsView.getChildCount(); i++) {
  buttons.add((Button) buttonsView.getChildAt(i));
}

Also you can store button's number in it's tag so you don't need to create final int variables:

ViewGroup buttonsView = (ViewGroup) findViewById(R.id.buttons);
List<Button> buttons = new ArrayList<Button>();
View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent myIntent = new Intent(Start.this, MainActivity.class);
        myIntent.putExtra("players", (Integer) v.getTag());
        startActivity(myIntent);
        //startActivity(new Intent(Start.this, MainActivity.class));
    }
};
for (int i = 0; i < buttonsView.getChildCount(); i++) {
  Button button = (Button) buttonsView.getChildAt(i);
  button.setTag(i);
  button.setOnClickListener(listener);
  buttons.add(buttons);
}
Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
  • Just want to clarify, this only works if the `ViewGroup` contains nothing but all the buttons. It cannot contains any other childs. – Lawrence Choy Dec 19 '12 at 09:39
  • Thanks for taking time to awnser my question Nikita, but it's a bit beyond my understanding since I'm quite new to programming. – Epcilon Dec 21 '12 at 19:18