How To Add Multiple Buttons To A Row Dynamically In Android
Does anybody know how to add multiple buttons to a table row dynamically in Android?
Solution 1:
You can try this and see if it's what your are looking for.
main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/layout"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"
><Buttonandroid:text="Button"android:id="@+id/button1"android:layout_height="wrap_content"android:layout_width="wrap_content"/><TableLayoutandroid:id="@+id/tableLayout1"android:layout_height="wrap_content"android:layout_width="fill_parent" /></LinearLayout>
Your Activity class:
publicclassmainActivityextendsActivityimplementsOnClickListener {
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.main );
Buttonb= (Button) findViewById( R.id.button1 );
b.setOnClickListener( this );
}
@OverridepublicvoidonClick(View v) {
TableLayouttable= (TableLayout) findViewById( R.id.tableLayout1 );
intbuttonsInRow=0;
intnumRows= table.getChildCount();
TableRowrow=null;
if( numRows > 0 ){
row = (TableRow) table.getChildAt( numRows - 1 );
buttonsInRow = row.getChildCount();
}
if( numRows == 0 || buttonsInRow == 3 ){
row = newTableRow( this );
table.addView( row );
buttonsInRow = 0;
}
if( buttonsInRow < 3 ){
Buttonb=newButton( this );
row.addView( b, 100, 50 );
}
}
}
Hope it helps.
Solution 2:
Here layout is a TableLayout.If you want to add a row dynamically and buttons in that row can use the follwoing code
TableRow tr1=newTableRow(this);
Button tv=newButton(this);
tv.setText("");
tr1.addView(tv,250,30);
Button tv1=newButton(this);
tv1.setText("");
tr1.addView(tv1,100,30);
layout.addView(tr1);
If you already have row in the layout then just fetch the row and add buttons to the row
Solution 3:
After searching for 30 sec I found http://www.warriorpoint.com/blog/2009/07/01/android-creating-tablerow-rows-inside-a-tablelayout-programatically - this should help you, just change TextViews to Buttons.
Post a Comment for "How To Add Multiple Buttons To A Row Dynamically In Android"