Flakpanda
Flakpanda

Reputation: 1

Textview and TableRow Weights Not Working Properly Android

For the life of me I cannot figure out why my text is ending up like this. I am new to java/android programming, so I apologize if the answer doesn't seem obvious to me. I have a tablelayout with one static row and the rest dynamically programmed in. The static row looks fine, but my dynamic row have the 1st textview squished even though weight is set to 1 for both textviews. Can someone please enlighten me as to what I am doing/not doing?

XML:

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="16dp"
        android:id="@+id/alchemist_favored_class">

        <TableRow>

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Race"
                android:gravity="left"
                android:padding="2dp" />

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Bonus"
                android:gravity="left"
                android:padding="2dp" />

        </TableRow>

    </TableLayout>

Java:

//Populate Favored Class Options Table
    TableLayout tableFC = (TableLayout) findViewById(R.id.alchemist_favored_class);
    String[] fcr = getResources().getStringArray(R.array.alchemist_favored_class_race);
    String[] fcd = getResources().getStringArray(R.array.alchemist_favored_class_detail);

    TableRow.LayoutParams lp1 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);
    TableRow.LayoutParams lp2 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);

    for (int i = 0; i < fcr.length; i++){

        TableRow tr = new TableRow(this);
        TextView tvFCR = new TextView(this);
        TextView tvFCD = new TextView(this);

        tr.addView(tvFCR);
        tvFCR.setText(fcr[i]);
        tvFCR.setSingleLine(true);
        tvFCR.setPadding(2,2,2,2);
        tvFCR.setLayoutParams(lp1);

        tr.addView(tvFCD);
        tvFCD.setText(fcd[i]);
        tvFCD.setPadding(2,2,2,2);
        tvFCD.setLayoutParams(lp2);

        //Add background color to every other row.
        if ((i % 2) == 0) {
            tr.setBackgroundColor(Color.parseColor("#EEEEEE"));
        }

        tableFC.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));

    }

Result:

Imgur

Upvotes: 0

Views: 446

Answers (2)

Flakpanda
Flakpanda

Reputation: 1

Removing the weight from the first textview in the row fixed the issue and now displays as desired.

Image

Upvotes: 0

Saumik Bhattacharya
Saumik Bhattacharya

Reputation: 941

Put something like this --

tableFC.setStretchAllColumns(true);

Hope this helps!

Upvotes: 1

Related Questions