0

In my android project, I had a table view and I want to add some image buttons dynamically. So I code like this

TableLayout table = (TableLayout)findViewById(R.id.tableLayout_1);

TableRow rows = new TableRow(this);
rows.setBackgroundColor(Color.parseColor("#eaeaea"));
rows.setId(4500+id);

ImageButton btns=new ImageButton(this);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            params1.leftMargin = 10;
            params1.bottomMargin = 10;

            btns.setBackgroundResource(R.drawable.accept);

            btns.setId(id);

               btns.setOnClickListener(new OnClickListener() {

                   @Override
                   public void onClick(View v) {
                       int id=v.getId();
                       setFlag(id);
                   }
               });


LinearLayout childLayout = new LinearLayout(context);
childLayout.addView(btns);
rows.addView(childLayout);  
table.addView(rows);

Its working fine. But the number of image buttons may vary. Sometimes 5-10 buttons That time all the buttons are showing in a single row. I want to expand the linearlayout and tablerow layout according to the buttons like the image.

enter image description here

Any idea for that ?

I am new to android please help

Thanks in advance

ramesh
  • 4,008
  • 13
  • 72
  • 117

1 Answers1

0

Try to use GridView, it's more recommended for what you're trying to do. Here is how to use. http://developer.android.com/guide/topics/ui/layout/gridview.html

If you have trouble at implementing, just reply here.

George
  • 271
  • 3
  • 12
  • hi thanks for the reply ... can you show a dynamic code which will create grid view and add image buttons ? – ramesh Aug 28 '13 at 16:11
  • The link from android developer is straight forward, you can just copy paste from there in order to implement gridview. After you have a list of items displayed and you want to add more, just call gridViewAdapter.add(yourItem) and then gridViewAdapter.notifydatasetchanged() – George Aug 28 '13 at 20:04
  • Hi actually I need to align some image buttons in a linearlayout and need to increase the height of the linearlayout according to the inner buttons. Now its showing only one row of image buttons. ie if the width can capable of 4 buttons then the 5th button must comes under 1 and so on.. But now only 4 buttons are showing – ramesh Aug 29 '13 at 03:25