Home > Net >  Android (Java): Programatically adding TextView(s) with weight 1 to a TableRow does not result in Te
Android (Java): Programatically adding TextView(s) with weight 1 to a TableRow does not result in Te

Time:04-11

In my Android application, I am attempting to programatically add a TableRow to a TableLayout in which the TableRow has TextView(s) of equal weight (1), meaning they occupy the same width. Below is an image showing what I am trying to achieve (I am trying to programatically add the TableRow highlighted in red):

enter image description here

However, below is the result of my code after programmatically adding a TableRow with 3 TextView(s) of equal weight 1:

enter image description here

Below is the XML for the main layout, and the template used to programmatically create the TextView of weight 1. The error seems strange, considering that in the template used to programatically create the TextView, I specified the weight as 1.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TableLayout
        android:id="@ id/table_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp">
        <TableRow
            android:background="#d6d7d7"
            android:padding="10dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="A"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="B"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="C"
                android:textColor="#000000" />
        </TableRow>
    </TableLayout>
</LinearLayout>

table_textview.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColor="#000000" />

MainActivity.java:

package com.example.programaticallyaddtablerowtotablelayout;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Retrieve of the table layout.
        TableLayout table = (TableLayout) findViewById(R.id.table_layout);

        // Initialize the row to be added to the table layout.
        TableRow newRow = new TableRow(this);
        newRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));

        // Add the TextView(s) to the row.
        TextView xView = (TextView) createViewFromXMLTemplate(R.layout.table_textview);
        xView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        xView.setText("X");
        newRow.addView(xView);

        TextView yView = (TextView) createViewFromXMLTemplate(R.layout.table_textview);
        yView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        yView.setText("Y");
        newRow.addView(yView);

        TextView zView = (TextView) createViewFromXMLTemplate(R.layout.table_textview);
        zView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        zView.setText("Z");
        newRow.addView(zView);

        // Add the row to the table layout.
        table.addView(newRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
    }

    // Creates a View by inflating a template XML layout.
    private View createViewFromXMLTemplate(int layout) {
        return getLayoutInflater().inflate(layout, null);
    }
}

CodePudding user response:

you have to do like below to get weight over TextView

TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));

CodePudding user response:

Try using setColumnStretchable (int columnIndex, boolean isStretchable)

  • Related