Question

I am trying to dynamically generate my activity layout but for some reason I can't get my TableLayout or my TableRow to match the parent width. I am using a relative layout, then I create my TableLayout and fill it with rows before adding my TableLayout to my RelativeLayout.

Here is my onCreate:

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

    context = this;

    RelativeLayout mainLayout = new RelativeLayout(context);
    RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT
    );
    mainLayout.setLayoutParams(relativeLayout);

    //Create the signature views
    //createPage(mainLayout);

    setupPage(mainLayout);

    setContentView(mainLayout);
}

Here is my setupPage() code:

private void setupPage(RelativeLayout mainLayout)
{
    TableLayout mainTable = new TableLayout(context);
    RelativeLayout.LayoutParams tableLayoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );
    mainLayout.setLayoutParams(tableLayoutParams);
    mainTable.setBackgroundResource(R.drawable.rectangle);

    //JOB DESCRIPTION title
    mainTable.addView(getRowTitle("JOB DESCRIPTION"));

    //----   END   ----
    mainLayout.addView(mainTable);
}

private TableRow getRowTitle(String text)
{
    TableRow row = new TableRow(context);
    TableLayout.LayoutParams rowLayout = new TableLayout.LayoutParams(
            TableLayout.LayoutParams.MATCH_PARENT,
            TableLayout.LayoutParams.MATCH_PARENT
    );
    row.setLayoutParams(rowLayout);
    row.setBackgroundResource(R.drawable.rectangle);//so i can see the outline of the row

    /*TextView title = new TextView(context);
    TableRow.LayoutParams editLayout = new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.WRAP_CONTENT
    );
    editLayout.setMargins(15,0,0,0);
    title.setLayoutParams(editLayout);
    title.setText(text);

    row.addView(title);*/

    return row;
}
Was it helpful?

Solution

You're not setting the LayoutParams on the proper view(which most likely ends up with the default LayoutParams). In the setupPage() method you do:

mainLayout.setLayoutParams(tableLayoutParams);

instead of setting it on the TableLayout:

mainTable.setLayoutParams(tableLayoutParams);

OTHER TIPS

try this -

private void setupPage(RelativeLayout mainLayout)
{
TableLayout mainTable = new TableLayout(context);
RelativeLayout.LayoutParams tableLayoutParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT
);
mainTable.setLayoutParams(tableLayoutParams);
mainTable.setBackgroundResource(R.drawable.rectangle);

//JOB DESCRIPTION title
mainTable.addView(getRowTitle("JOB DESCRIPTION"));

//----   END   ----
mainLayout.addView(mainTable);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top