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 6944849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:21:38+00:00 2026-05-27T13:21:38+00:00

Context: I have a TableLayout (created using XML), which has one TableRow, which has

  • 0

Context: I have a TableLayout (created using XML), which has one TableRow, which has one TextView. The code:

 <ScrollView 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:fillViewport="true"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
  <TableLayout
     android:id="@+id/mytable"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:stretchColumns="1"
     >
    <TableRow>
      <TextView
     android:id="@+id/add_alarm"
     android:layout_weight="1"
     android:layout_width="0dp"
     android:layout_height="fill_parent"
     android:gravity="center"
     android:text="New\nItem"
     android:textSize="30sp"
     />
    </TableRow>
  </TableLayout>
</ScrollView>

In my Activity’s onCreate() method, I am trying to add another View to the TableRow dynamically. Here is the code:

    public void onCreate(Bundle savedInstanceState)    {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(this);
        View mainLayout = inflater.inflate(R.layout.main, null);
        TableLayout tl = (TableLayout) mainLayout.findViewById(R.id.mytable);

        TableRow tr = (TableRow) tl.getChildAt(0);
        Log.d(TAG, "tr class = " + tr.getClass().getName() + " | width = " + tr.getWidth() + "\n");
        final RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.alarm_widget, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
        tr.addView(rl, lp); 
        tl.invalidate();
        setContentView(mainLayout);
}

Question: This code is not having the intended effect of displaying both the Views (the one in the XML layout already & the other added dynamically) in a columns of equal width.

  1. With the code given above, the dynamically added View has a width of ‘0’ and is therefore invisible.
  2. If I change the code to tr.addView(rl) (i.e. without reference to LayoutParams), the dynamically added view is visible, but the columns are not equal in width.

How can I achieve this?

Edit: I changed the code based on the comments to the following. It still doesn’t work as expected:

TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                               TableLayout.LayoutParams.WRAP_CONTENT, 1f);
tr.addView(rl, lp); 
  • 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-27T13:21:39+00:00Added an answer on May 27, 2026 at 1:21 pm

    The problem is this behavior defined for TableRow:

    The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.

    Rather than add your text views directly to the TableRow, have the TableRow hold a horizontal LinearLayout and add the second view to that holder.

    (Also, using LinearLayout.LayoutParams for something that’s going into a TableRow is wrong. You should have been using TableRow.LayoutParams. But that wouldn’t be the way to get equal-width TextViews. Use a LinearLayout holder.)

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

Sidebar

Related Questions

Hi all I have created layout using following code <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
i have created following layout. <TableLayout android:layout_width=fill_parent android:layout_height=wrap_content android:orientation=horizontal > <TableRow> <TextView android:layout_width=fill_parent android:layout_height=wrap_content
I have a table layout: <TABLELAYOUT xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=fill_parent> <TABLEROW> <TEXTVIEW android:layout_width=wrap_content android:layout_height=wrap_content android:text=1>
I have a context loader class which loads an XML file with info on
I have an android activity in which I'm using tabs. public class UnitActivity extends
I have a plain vanilla layout: <ScrollView> <TableLayout> <TableRow> ... but the content gets
I have created two tabs in my Activity using the below code. This is
I have a TableLayout that is created programmatically in an Android project. I keep
I have a Layout (using TableLayout, etc...) which works well. It seems like this:
I have created a table layout in xml. At runtime, i am using a

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.