List of button in Android

17,378

I think you should make buttons dinamically.. like this

Button[] btnWord = new Button[num];
 LinearLayout linear;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dynamicview);
  test();
 }
 private void test() {  
  linear = (LinearLayout) findViewById(R.id.linear);
  for (int i = 0; i < btnWord.length; i++) {
   btnWord[i] = new Button(this);
   btnWord[i].setHeight(50);
   btnWord[i].setWidth(50);
   btnWord[i].setTag(i);
   btnWord[i].setOnClickListener(btnClicked);
   linear.addView(btnWord[i]);
  }
 }
 OnClickListener btnClicked = new OnClickListener() {
  @Override
  public void onClick(View v) {
   Object tag = v.getTag();
   Toast.makeText(getApplicationContext(), "clicked button", Toast.LENGTH_SHORT).show();
  }
 };

you can change the number of button array

Button[] btnword = new Button[num];
Share:
17,378

Related videos on Youtube

Tanasis
Author by

Tanasis

Updated on July 08, 2022

Comments

  • Tanasis
    Tanasis almost 2 years

    I want to make a list of ImageButtons in an Activity with three buttons in each row. If I make this with XML (considering there are over 100 buttons) eclipse complains that there are to many views.

    Is there a better way to do this? Thanks!

    • Hardik Joshi
      Hardik Joshi almost 11 years
      Create buttons dynamically.