| * | Layout table for 3 x 3 buttons using Java code:
Set row count to tblRowCwtVal
Set the number of columns to tblColCwtVal
Set line | Ability to draw in tblAryVar
In this example, we used a button for each kind of table. You can use TextView | ImageView and change accordingly
int tblRowCwtVal = 3; int tblColCwtVal = 3; int[][] tblAryVar = { {R.drawable.ic_name, R.drawable.ic_name, R.drawable.ic_name}, {R.drawable.ic_name, R.drawable.ic_name, R.drawable.ic_name}, {R.drawable.ic_name, R.drawable.ic_name, R.drawable.ic_name} }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.srn_nam_uic); namRelLyoVar = (RelativeLayout) findViewById(R.id.NamSrnLyoUid); TableLayout namTblLyoVar = new TableLayout(this); TableLayout.LayoutParams tblLyoRulVar = new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); TableRow.LayoutParams btnLyoRulVar = new TableRow.LayoutParams(50,50); for(int tblRowIdxVar = 0; tblRowIdxVar < tblRowCwtVal; tblRowIdxVar++) { TableRow tblRowVar = new TableRow(this); for(int tblColIdxVar = 0; tblColIdxVar < tblColCwtVal; tblColIdxVar++) { Button namIdxBtnVar = new Button(this); Drawable DrwablIdxVar = getResources().getDrawable(tblAryVar[tblRowIdxVar][tblColIdxVar]); DrwablIdxVar.setColorFilter(Color.rgb(0,128,0), PorterDuff.Mode.SRC_IN); namIdxBtnVar.setBackground(DrwablIdxVar); tblRowVar.addView(namIdxBtnVar, btnLyoRulVar); } namTblLyoVar.addView(tblRowVar, tblLyoRulVar); } namTblLyoVar.setLayoutParams(tblLyoRulVar); namRelLyoVar.addView(namTblLyoVar); }
source share