Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7840665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:51:55+00:00 2026-06-02T15:51:55+00:00

New to android programming. I have a layout XML, to which I want to

  • 0

New to android programming.

I have a layout XML, to which I want to add a list (with items from a database), formatted as a table.

Here’s the code:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.edit_cat);

        DatabaseHandler db = new DatabaseHandler(this);
        List<Category> allCategories = db.getAllCategories();

        //Get the main layout from XML
        LinearLayout mainLayout = (LinearLayout) findViewById(R.id.edit_cat);

        //Create table layout for the categories
        TableLayout catList = new TableLayout(this);

        //Iterate the categories, and organize in tables
        for (Category category : allCategories) {
            TableRow tr = new TableRow(this);

            //Display category name
            TextView name = new TextView(this);
            name.setLayoutParams(new LayoutParams(                      
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            name.setText(category.getName());


            //Display category type
            TextView type = new TextView(this);
            type.setText(category.getType());
            type.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            //Display edit button
            Button btnEdit = new Button(this);
            btnEdit.setText(getResources().getString(R.string.edit));

            btnEdit.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            //Display delete button
            Button btnDelete = new Button(this);
            btnDelete.setText(getResources().getString(R.string.delete));

            btnDelete.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));


            tr.addView(name);
            tr.addView(type);
            tr.addView(btnEdit);
            tr.addView(btnDelete);

            catList.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
        }

        mainLayout.addView(catList);


    }

This does not add anything to the layout. Any ideas why this does not work?

EDIT: Added the xml code here:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit_cat"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">

<TextView style="@style/pageHeader"
  android:text="@string/catEditor"/>

<Button
    android:id="@+id/btnNewCategory"
    android:text="@string/newCategory"
    android:textSize="11pt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="onBtnClicked"/>
<Button
    android:id="@+id/back"
    android:text="@string/back"
    android:textSize="11pt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="onBtnClicked"/>

</LinearLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:51:58+00:00Added an answer on June 2, 2026 at 3:51 pm

    Ok, I think I have an answer for you.

    You need to be more specific when you are specifying LayoutParams. While it doesn’t throw a compile exception, if you don’t specify the correct parent class of the LayoutParams, nothing will show up.

    When you specify layout params, you need to use the class of the container where the object is going. If you are setting the layout paramters of a textview that is going inside a LinearLayout, you would use new LinearLayout.LayoutParams(...) similarily if you are creating a textview to go inside a TableRow, you need to use TableRow.LayoutParams(...)

    Below is the full code with minor modifications because I didn’t have a db setup.

        LinearLayout mainLayout = (LinearLayout) findViewById(R.id.edit_cat);
    
        //Create table layout for the categories        
        TableLayout catList = new TableLayout(this);
    
        //Set the table layout to Match parent for both width and height
        // Note: We use LinearLayout.LayoutParams because the TableLayout is going inside a LinearLayout
    
        catList.setLayoutParams(
            new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    
    
        //Iterate the categories, and organize in tables      
        for(int i = 0; i < 5; i++)
        {
            TableRow tr = new TableRow(this);
            /* Since the table row is going inside a table layout, we specify the parameters using TableLayout.LayoutParams */
            tr.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    
            //Display category name
            TextView name = new TextView(this);
    
            /* Since the TextView is going inside a TableRow, we use new TableRow.LayoutParams ... */
            name.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
    
            name.setText("Test Row - " + i);
            name.setBackgroundColor(Color.RED);
    
    
            //Display category type
            TextView type = new TextView(this);
            type.setText("Type Row - " + i);
            type.setBackgroundColor(Color.GREEN);
            type.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
    
            //Display edit button
            Button btnEdit = new Button(this);
            btnEdit.setText("Edit");
    
            btnEdit.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
    
            //Display delete button
            Button btnDelete = new Button(this);
            btnDelete.setText("Delete");
    
            btnDelete.setLayoutParams(new TableRow.LayoutParams(
                     TableRow.LayoutParams.FILL_PARENT,
                     TableRow.LayoutParams.WRAP_CONTENT));
    
    
            tr.addView(name);
            tr.addView(type);
            tr.addView(btnEdit);
            tr.addView(btnDelete);
    
            catList.addView(tr);
        }
    
    
        mainLayout.addView(catList);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Android and Java programming. I have a class which implements
I am new to android programming. I have a background in my layout file
I am new to XML, and Android programming.I was following a tutorial, which asked
I am new to Android/java programming. I really have no clue what I am
I'm new to Android & Java programming but have been programming in .NET for
I am new to Eclipse and Android programming in general but I have been
I'm abit new to programming Android App's, however I have come across a problem,
I am very new to Android programming, and I have read everywhere and I
I am new to Android programming. I have installed the Eclipse and Android SDK.
I'm VERY new to android programming, so please forgive. I have an assignment in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.