Question

I am trying to create a table with 1 row, first column is the name of a header, and the next column is a little imageView icon. I can't figure out why nothing is showing.

This is my code. I will show the xml I am trying to replicate below.

Here I'm just trying to create 1 table. Then I was planning on using a for loop to make multiple tables with different headers( this is for future use. )

RelativeLayout relative = new RelativeLayout(context);

        //set layout
        LayoutParams relativeLayoutParams = new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT
        );
        relative.setLayoutParams(relativeLayoutParams);

        //create table for each header
        TableLayout table = new TableLayout(context);
        TableRow tableRow = new TableRow(context);

        //set layout
        LayoutParams tableLayoutParams = new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
        );
        LayoutParams tableRowLayout = new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
        );

        tableLayoutParams.setMargins(15, 10, 0, 0);
        table.setBackgroundResource(R.drawable.rectangle);

        //Set layout params for Table & Row
        table.setLayoutParams(tableLayoutParams);
        tableRow.setLayoutParams(tableRowLayout);

        //Create textView to contain header Title and icon
        TextView tvHeader = new TextView(context);
        ImageView infoIcon = new ImageView(context);

        LayoutParams headerLayoutParams = new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
        );
        LayoutParams iconLayoutParams = new LayoutParams(20,20);
        iconLayoutParams.setMargins(10, 0, 0, 0);

        //set icon image
        infoIcon.setBackgroundResource(R.drawable.information);

        // Set textView layout params
        tvHeader.setLayoutParams(headerLayoutParams);
        // Set icon layout params
        infoIcon.setLayoutParams(iconLayoutParams);

        //Assign the header text
        tvHeader.setText("testHeader");

        //Add the TextView & Icon to the row
        tableRow.addView(tvHeader);
        tableRow.addView(infoIcon);

        //Add the rows to the table
        table.addView(tableRow);

        relative.addView(table);

        setContentView(relative);

Here is the xml I am trying to generate

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/rectangle"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp">

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="column1"
            android:id="@+id/textView" />

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:text="column2"
            android:id="@+id/image"
            android:src="@drawable/information"
            android:layout_marginLeft="15dp" />
    </TableRow>
</TableLayout>
</RelativeLayout>
Was it helpful?

Solution

I've managed to figure out the problem. The reason why nothing was showing was because I would create a LinearLayout layout parameter for my TextView, when instead I should have been using "TableRow.LayoutParams" because it's the parent. Here is a similar type answer I found which reconfirmed my suspicion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top