android – TableLayout not Showing Content

Exception or error:

I’m trying to create a news feed test that gets loaded in a table (once) when the application starts

the text view which contains the text is not showing, and I can’t find the issue

the table is now showing any content, and I have no idea if the rows are added or not


import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup;
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);

        loadFeed();
    }

    public void loadFeed() {
        TableLayout tabitha = findViewById(R.id.tabitha);
        TableRow tablerow_a = new TableRow(this);
        tablerow_a.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.MATCH_PARENT));

        TableRow tablerow_b = new TableRow(this);
        tablerow_b.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));

        TextView textview_a = new TextView(this);
        textview_a.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
        textview_a.setText("Andrew Richard has uploaded:");
        textview_a.setTextColor(Color.BLACK);
        textview_a.setTextSize(2,25);

        tabitha.addView(tablerow_a);
        tabitha.addView(tablerow_b);

        tablerow_a.addView(textview_a);

        setContentView(R.layout.activity_main);
    }
}

And here is the XML file in question

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/contentConLay"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#5E2B2B2B"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TableLayout
            android:id="@+id/tabitha"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *