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

  • Home
  • SEARCH
  • 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 6159013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:03:33+00:00 2026-05-23T21:03:33+00:00

I have a TableLayout in my app, which I need to define and add

  • 0

I have a TableLayout in my app, which I need to define and add programmatically, as it builds upon the output of an SQL query. Basically, the result should look similar to a timetable (7 columns for each day in a week, multiple rows for different timeslots per day). For the cells/timeslots, I just need ordinary views with a certain background colour.

The problem is, that all views end up with a height and width of 0, although defined otherwise. What am I doing wrong?

Code:

RelativeLayout.LayoutParams tableLayout = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tableLayout.addRule(RelativeLayout.BELOW, R.id.myOtherLayout);
tableLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);

TableLayout table = new TableLayout(this);
table.setBackgroundColor(Color.WHITE);
table.setMinimumHeight(PixelConverter.pxToSp(this, 240));

// get availability table data
int[][] tableData = getTableData(...);

if (availability != null) { 
    // init array of tablerows, which represent one line each for every timeslot
    for (int timeSlot = 0; timeSlot < 12; h++) {
        // init row
        TableRow newRow = new TableRow(this);
        newRow.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT, // width
            10)); // height
        newRow.setMinimumHeight(10);

        // create 7 views in each row - one for each day in a week
        for (int day = 0; day < 7; d++) {
            View v = new View(this);
            v.setLayoutParams(new LinearLayout.LayoutParams(
                10, // TODO width
                10)); // height
            v.setMinimumWidth(10);
            v.setMinimumHeight(10);

            int cellHue = tableData[day][timeSlot];
            if (cellHue >= 0) {
                v.setBackgroundColor(Color.HSVToColor(new float[] { cellHue, 100, 100 }));
            } else {
                v.setBackgroundColor(Color.TRANSPARENT);
            }

            // add view to tablerow
            newRow.addView(v, day);
        }

        table.addView(newRow, timeSlot);
    }       
}

table.setId(R.id.my_table_id);
mainLayout.addView(table, tableLayout);
  • 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-05-23T21:03:34+00:00Added an answer on May 23, 2026 at 9:03 pm

    I’ve encountered a similar issue – the problem appears to be with the choice of type of LayoutParams used after inflating your views. Particularly, the following is probably the root of your problem:

    View v = new View(this);
            v.setLayoutParams(new LinearLayout.LayoutParams(
                10, // TODO width
                10)); // height
    

    I’d suggest that you try:

    View v = new View(this);
            v.setLayoutParams(new TableRow.LayoutParams(
                10, // TODO width
                10)); // height
    

    The key is that you apparently should specify the type of the parent view when using LayoutParams, rather than the type of the view that you’re setting them on. Note that in this case, you’ll probably need to specify TableLayout.LayoutParams when calling setLayoutParams on your row, too.

    I don’t know how well documented this is – I noticed it mentioned on an arbitrary blog I’d found (lost the link now, alas) and I haven’t had chance to go back to the official documention yet, but presumably it’s there somewhere… 🙂

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a input tag which is non editable, but some times i need
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have a TableLayout defined in my xml with three columns and four rows
I have a TableLayout with a tableRow and a horizontal scroll view in the
I have four buttons arranged in a 2x2 TableLayout. These buttons each have an
I have a new web app that is packaged as a WAR as part
I have a login.jsp page which contains a login form. Once logged in the
Have just started using Google Chrome , and noticed in parts of our site,
Have you guys had any experiences (positive or negative) by placing your source code/solution

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.