AndroidDev
AndroidDev

Reputation: 4559

How to create Array of buttons in android using java

I want to create an array of buttons in my app. So how that can be done. Secondly, is it possible to interact with each button in an array of buttons? If so, how? Please suggest to me.

Regards
Anshuman

Upvotes: 3

Views: 7576

Answers (1)

Dayerman
Dayerman

Reputation: 4003

Inside you Activity do something like this:

    LinearLayout linear = new LinearLayout(this);
    linear.setOrientation(LinearLayout.VERTICAL);       

    ArrayList<Button> ab = new ArrayList<Button>();
    for (Button b : ab) {       
        linear.addView(b);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
    };
    setContentView(linear);

Upvotes: 3

Related Questions