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));
            }
        });

    }
}
4
  • 3
    bt[i] will be replaced with bt[1] and not bt1 for i = 1. You can't use variable like that. Commented Dec 19, 2012 at 9:28
  • @RohitJain that's why OP asks the question. Commented Dec 19, 2012 at 9:32
  • here's one solution for your problem stackoverflow.com/questions/3648942/… Commented Dec 19, 2012 at 9:34
  • @RohitJain Thanks for clearifying what the bt[i] really does! Commented Dec 19, 2012 at 18:32

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Simple enough to understand and indeed beautiful. I'll give it a try as soon as I start my comp. tomorrow!
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);
}

2 Comments

Just want to clarify, this only works if the ViewGroup contains nothing but all the buttons. It cannot contains any other childs.
Thanks for taking time to awnser my question Nikita, but it's a bit beyond my understanding since I'm quite new to programming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.