Code example for TableLayout

Methods: addView, setLayoutParams

0
	} 
 
 
	private TableLayout addTable(String name, String brand, String caption, String type, String counter){
	     
		TableLayout table = new TableLayout(this);
	     
	    //Setting table parameters 
	    LinearLayout.LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
	    layoutParams.setMargins(0, 5, 0, 5);
	    table.setLayoutParams(layoutParams);
	     
	    
	    //Create a new row to be added. 
	    TableRow tr1 = new TableRow(this);
	    tr1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	         
	    //Create the textView for name 
	    TextView textView1 = new TextView(this);
	    textView1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	    textView1.setText("Name: " + name);
	    	 
	    //Add textView to tableRow 
	    tr1.addView(textView1);
	         
	    //Add tableRow to table 
	    table.addView(tr1);
	     
	    //Create a new row to be added. 
	    TableRow tr2 = new TableRow(this);
	    tr2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	         
	    //Create the textView for caption 
	    TextView textView2 = new TextView(this);
	    textView2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	    textView2.setText("Brand: " + brand);
	    	 
	    //Add textView to tableRow 
	    tr2.addView(textView2);
	         
	    //Add tableRow to table 
	    table.addView(tr2);
	     
	    //Create a new row to be added. 
	    TableRow tr3 = new TableRow(this);
	    tr3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	         
	    //Create the textView for caption 
	    TextView textView3 = new TextView(this);
	    textView3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	    textView3.setText("Caption: " + caption);
	    	 
	    //Add textView to tableRow 
	    tr3.addView(textView3);
	         
	    //Add tableRow to table 
	    table.addView(tr3);
	     
	    //Create a new row to be added. 
	    TableRow tr4 = new TableRow(this);
	    tr4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	         
	    //Create the textView for caption 
	    TextView textView4 = new TextView(this);
	    textView4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	    textView4.setText("Type: " + type);
	    	 
	    //Add textView to tableRow 
	    tr4.addView(textView4);
	         
	    //Add tableRow to table 
	    table.addView(tr4);
	     
	  //Create a new row to be added. 
	    TableRow tr5 = new TableRow(this);
	    tr5.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	         
	    //Create the textView for caption 
	    TextView textView5 = new TextView(this);
	    textView5.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	    textView5.setText("Wore it: " + counter + " times");
	    	 
	    //Add textView to tableRow 
	    tr5.addView(textView5);
	         
	    //Add tableRow to table 
	    table.addView(tr5);
	     
	     
	    return table;
	}