Android TableLayout programmatically

I learned how to create a user interface using an XML file. But please help me learn how to do this programmatically without using XML files, especially for LinearLayout.

+4
source share
3 answers

Use the following code to create a TableLayout

TableLayout tbl=new TableLayout(context); 

Use below to create a table row

 TableRow tr=new TableRow(context); 

add view to table row

 tr.addView(view); 

here the view can be TextView or EditText or ect ..

add row table to TableLayout

 tbl.addView(tr); 

Similarly, you can add more table rows to the table of tables.

+14
source

Below is a sample code here .

 public class tablelayout extends Activity implements OnClickListener { /** Called when the activity is first created. */ //initialize a button and a counter Button btn; int counter = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setup the layout setContentView(R.layout.main); // add a click-listener on the button btn = (Button) findViewById(R.id.Button01); btn.setOnClickListener(this); } // run when the button is clicked public void onClick(View view) { // get a reference for the TableLayout TableLayout table = (TableLayout) findViewById(R.id.TableLayout01); // create a new TableRow TableRow row = new TableRow(this); // count the counter up by one counter++; // create a new TextView TextView t = new TextView(this); // set the text to "text xx" t.setText("text " + counter); // create a CheckBox CheckBox c = new CheckBox(this); // add the TextView and the CheckBox to the new TableRow row.addView(t); row.addView(c); // add the TableRow to the TableLayout table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } 

}

+4
source

| * | 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); } 
0
source

Source: https://habr.com/ru/post/1369429/


All Articles