user3265560 user3265560 - 5 months ago 9
Java Question

TableLayout Without XML, Nothing Appears on Screen

I was trying to create

TableLayout
Without XML, Nothing Appears on Screen. Below is my code. Please let me know what is the mistake?

Code :

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.act);
TableLayout tlMain =(TableLayout)findViewById(R.id.tlMain);
LayoutInflater inflater = getLayoutInflater();

FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
tlMain.setLayoutParams(lp);


tlMain.setStretchAllColumns(true);
tlMain.setShrinkAllColumns(true);


TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
rowLp.weight=1;

TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);

for(int i=0;i<4;i++)
{
TableRow tableRow = new TableRow(this);

for (int j = 0; j < 4; j++) {
TextView number2 = new TextView(this);
number2.setTextColor(color.black);
number2.setText(String.valueOf(i));
tableRow.addView(number2,cellLp);
tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
}
tlMain.addView(tableRow,rowLp);
}
setContentView(tlMain);
}

Answer
    This is How it started Working.

    @Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 TableLayout table = new TableLayout(this);  
     table.setStretchAllColumns(true);
     table.setShrinkAllColumns(true);
     TableRow rowTitle = new TableRow(this);
     rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);
     TableRow.LayoutParams trparams = new   TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT);
    rowTitle.setLayoutParams(trparams); 
    rowTitle.setGravity(Gravity.CENTER_HORIZONTAL); 
    TableRow rowDayLabels = new TableRow(this);
    TableRow rowAmounts = new TableRow(this);
    TextView empty = new TextView(this);
    empty.setText("Empty");
    TextView title = new TextView(this);
    title.setLayoutParams(trparams);
    table.setLayoutParams(trparams);
    title.setText("WikiCode");
    rowTitle.addView(title);
    rowTitle.addView(empty);
    table.addView(rowTitle);
    for(int i=0;i<4;i++)
    {   
        TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(trparams);
        for (int j = 0; j < 4; j++) {
            TextView number2 = new TextView(this);
            number2.setLayoutParams(trparams);               
            number2.setText("check"+i);                
            tableRow.addView(number2);
            tableRow.setGravity(Gravity.CENTER_HORIZONTAL);               
        }
        table.addView(tableRow);              
    }


    setContentView(table);
}