Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of button in Android

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!

like image 520
Tanasis Avatar asked Sep 30 '13 07:09

Tanasis


Video Answer


1 Answers

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];
like image 167
Ssam Avatar answered Sep 21 '22 13:09

Ssam