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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:55:57+00:00 2026-05-31T14:55:57+00:00

n.b.: I was going to post all this XML on Pastebin but I think

  • 0

n.b.: I was going to post all this XML on Pastebin but I think it’s important to have it archived here for posterity, so please pardon the code sample length! Better to have more info than less, right?

Consider the following calculator-ish layout. (I hard-coded the strings just for this post – they are normally resources.)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/calculator"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp"
    android:orientation="vertical"
    android:stretchColumns="*">

    <TableRow android:id="@+id/tableRowFieldName"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView android:id="@+id/textViewFieldName"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:layout_marginTop="0dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="0dp"
            android:layout_marginLeft="5dp"
            android:padding="0dp"
            android:text="Label"
            android:textSize="20dp"
            android:textColor="#FFFFFF"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </TableRow>

    <TableRow android:id="@+id/tableRowDisplay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView android:id="@+id/textViewDisplay"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:layout_margin="5dp"
            android:layout_marginTop="0dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="0dp"
            android:layout_marginLeft="5dp"
            android:padding="0dp"
            android:text="Value"
            android:textSize="35dp"
            android:textColor="#FFFFFF"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>

    <TableRow android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.2">

        <Button android:id="@+id/buttonA"
            style="@style/ParameterButton"
            android:text="Param A" />

        <Button android:id="@+id/buttonB"
            style="@style/ParameterButton"
            android:text="Param B" />
    </TableRow>

    <TableRow android:id="@+id/tableRow2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.2">

        <Button android:id="@+id/buttonC"
            style="@style/ParameterButton"
            android:text="Param C" />

        <Button android:id="@+id/buttonD"
            style="@style/ParameterButton"
            android:text="Param D" />
    </TableRow>

    <TableRow android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.2">

        <Button android:id="@+id/buttonE"
            style="@style/ParameterButton"
            android:text="Param E" />

        <Button android:id="@+id/buttonF"
            style="@style/ParameterButton"
            android:text="Param F" />
    </TableRow>

    <TableRow android:id="@+id/tableRow4"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.4">

        <ListView android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:visibility="gone"
            android:layout_weight="1.0" />

        <TableLayout android:id="@+id/calcKeypad"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:stretchColumns="*">

            <TableRow
                android:layout_weight="0.25"
                android:gravity="center">
                <Button android:id="@+id/button7"
                    style="@style/KeypadButton"
                    android:text="7" />
                <Button android:id="@+id/button8" 
                    style="@style/KeypadButton"
                    android:text="8" />
                <Button android:id="@+id/button9" 
                    style="@style/KeypadButton"
                    android:text="9" />
            </TableRow>

            <TableRow
                android:layout_weight="0.25"
                android:gravity="center">
                <Button android:id="@+id/button4"
                    style="@style/KeypadButton"
                    android:text="4" />
                <Button android:id="@+id/button5" 
                    style="@style/KeypadButton"
                    android:text="5" />
                <Button android:id="@+id/button6"
                    style="@style/KeypadButton"
                    android:text="6" />
            </TableRow>

            <TableRow
                android:layout_weight="0.25"
                android:gravity="center">
                <Button android:id="@+id/button1"
                    style="@style/KeypadButton"
                    android:text="1" />
                <Button android:id="@+id/button2" 
                    style="@style/KeypadButton"
                    android:text="2" />
                <Button android:id="@+id/button3" 
                    style="@style/KeypadButton"
                    android:text="3" />
            </TableRow>

            <TableRow
                android:layout_weight="0.25"
                android:gravity="center">
                <Button android:id="@+id/buttonClear"
                    style="@style/KeypadButton"
                    android:text="C" />
                <Button android:id="@+id/button0" 
                    style="@style/KeypadButton"
                    android:text="0" />
                <Button android:id="@+id/buttonDecimal" 
                    style="@style/KeypadButton"
                    android:text="." />
            </TableRow>
        </TableLayout>

        <LinearLayout android:id="@+id/linearLayoutTotal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <Button android:id="@+id/buttonTotalWeight"
                style="@style/FunctionButton"
                android:text="Function A" />
            <Button android:id="@+id/buttonTotalCost" 
                style="@style/FunctionButton"
                android:text="Function B" />
            <Button android:id="@+id/buttonAbout" 
                style="@style/FunctionButton"
                android:layout_height="0dp"
                android:layout_weight="0.25"
                android:text="Function C" />
        </LinearLayout>
    </TableRow>
</TableLayout>

This uses the following styles (essentially factored out of the original XML):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CalcButton">
        <item name="android:textColor">#ffffff</item>
        <item name="android:textSize">15dp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:layout_margin">2dp</item>
    </style>  
    <style name="ParameterButton" parent="CalcButton">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">0.5</item>
    </style>
    <style name="KeypadButton" parent="CalcButton">
        <item name="android:textSize">20dp</item>
    </style>
    <style name="FunctionButton" parent="CalcButton">
        <item name="android:layout_height">0dp</item>
        <item name="android:layout_weight">0.375</item>
    </style>
</resources>

If you were to drop all of this in to an Android project, you should see three rows of two parameter buttons up top, followed by a numeric keypad below that, with three function buttons to the right of the keypad. Mostly harmless. (Also, it will only ever be used in portrait – so no worries about taking landscape into consideration.)

However … pay particular attention to the ListView within the XML. You won’t see it in the visual just yet, since it has android:visibility="gone". The idea is that, at certain times, I will remove that, then set the numeric keypad (the adjacent TableLayout) to have android:visibility="gone" instead, thus showing the list in its place.

Put another way, the desired effect is to have the list and the numeric keypad occupy exactly the same space, such that I can flip back and forth between the two at will.

The problem I’m having is that the ListView, once populated, seems intent on taking up the entire height of the overall table, obliterating the poor parameter buttons and bringing the three function buttons along for the ride.

You can see this by rigging a very basic ArrayAdapter (I used android.R.layout.simple_list_item_1 and android.R.id.text1) and tossing a rudimentary String array its way. For now, manually move the gone assignment from the list over to the adjacent table layout (or do it in code).

Setting a fixed height seems out of the question, since this should be working on a variety of displays. In fact, I even tried what is perhaps a misguided endeavor:

ListView list = (ListView) findViewById(android.R.id.list);
TableLayout calcKeypad = (TableLayout) findViewById(R.id.calcKeypad);

list.layout(calcKeypad.getLeft(), calcKeypad.getTop(), calcKeypad.getRight(), calcKeypad.getBottom());
calcKeypad.setVisibility(View.GONE);
list.setVisibility(View.VISIBLE);

Nope, that didn’t help either.

In keeping with the documentation, I’ve even given ListView the proper ID since I want to customize its layout. Of course I’m not using a ListActivity, so perhaps it doesn’t matter in this case. (I’m just using a regular Activity. Yes, I tried changing that to ListActivity too. No dice.)

Still, I’m totally open to this being pilot error. (In fact, I’m counting on it.) I’m just at the point where it’s become head-hitting-the-wall-in-myopia mode. “There’s got to be a better way!”

Again, keep in mind I’m trying very hard to keep the layout fluid, so that it works well on a variety of display sizes (not tablets, just handhelds), hence the chosen layout. If there’s a better way that accomplishes the same visual, I’m all ears.

Bottom line: How do I make the ListView only take up the area occupied by that adjacent calculator keypad (TableLayout)?

Clues/answers/guidance appreciated!

  • 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-31T14:55:58+00:00Added an answer on May 31, 2026 at 2:55 pm

    There are some problems in your layout, but I didn’t spend a lot of time trying to sort out the various layout_weight and layout_height situations that you have. I think you might be able to get this to work, but I took a little different approach.

    I broke the layout up into a series of rows above the three elements at the bottom. Basically you have:

    TextView-------------------------------
    TextView-------------------------------
    LinearLayout---------------------------
    LinearLayout---------------------------
    LinearLayout---------------------------
    ---------------------------------------
    TableLayout and ListView | LinearLayout
    

    I’m not saying this is the most efficient layout, but every layout element has a job, so you might have to go to some lengths to replace them with something more efficient.

    In particular, the use of LinearLayout for your Parameter buttons and Function buttons makes good use of the layout_weight feature. It’s not available in other layouts that are not subclasses of LinearLayout (you may not be aware that TableRow is a subclass of LinearLayout, and that’s why layout_weight works there but not in RelativeLayout) and the ability to subdivide available space on a percentage basis seems to be appropriate for those elements.

    The TableLayout holding the numeric buttons could probably be replaced, but it’s doing a good job of allocating space for the buttons in different sized views, and it is also doing a nice job of spacing the rows to fill the available space. Again, it might be difficult to replace this.

    What I did was replace your overall TableLayout with a RelativeLayout with appropriate alignment attributes. That allows you to put the Function button LinearLayout against the right border and have the ListView or TableLayout consume the remaining space below the Parameter buttons and to the left of the Function buttons. I’m not saying you have to do this, necessarily, but it saves some extra layout elements over your original design and does the job you are looking for.

    Here’s a workup of what I did. Notice that I added a layout_height of wrap_content to your ParameterButton style. Also, both the ListView and TableLayout are visible in this layout. You may want adjust in the layout or in your code.

    The style:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="CalcButton">
            <item name="android:textColor">#ffffff</item>
            <item name="android:textSize">15dp</item>
            <item name="android:textStyle">bold</item>
            <item name="android:layout_width">fill_parent</item>
            <item name="android:layout_height">fill_parent</item>
            <item name="android:layout_margin">2dp</item>
        </style>  
        <style name="ParameterButton" parent="CalcButton">
            <item name="android:layout_height">wrap_content</item>
            <item name="android:layout_width">0dp</item>
            <item name="android:layout_weight">0.5</item>
        </style>
        <style name="KeypadButton" parent="CalcButton">
            <item name="android:textSize">20dp</item>
        </style>
        <style name="FunctionButton" parent="CalcButton">
            <item name="android:layout_height">0dp</item>
            <item name="android:layout_weight">0.375</item>
        </style>
    </resources>
    

    The layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/calculator"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp"
        android:orientation="vertical"
        android:stretchColumns="*">
            <TextView android:id="@+id/textViewFieldName"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:layout_marginTop="0dp"
                android:layout_marginRight="5dp"
                android:layout_marginBottom="0dp"
                android:layout_marginLeft="5dp"
                android:padding="0dp"
                android:text="Label"
                android:textSize="20dp"
                android:textColor="#FFFFFF"
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <TextView android:id="@+id/textViewDisplay"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:layout_margin="5dp"
                android:layout_marginTop="0dp"
                android:layout_marginRight="5dp"
                android:layout_marginBottom="0dp"
                android:layout_marginLeft="5dp"
                android:padding="0dp"
                android:layout_below="@id/textViewFieldName"
                android:text="Value"
                android:textSize="35dp"
                android:textColor="#FFFFFF"
                android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <LinearLayout android:id="@+id/tableRow1"
            android:layout_below="@id/textViewDisplay"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button android:id="@+id/buttonA"
                style="@style/ParameterButton"
                android:layout_height="wrap_content"
                android:text="Param A" />
            <Button android:id="@+id/buttonB"
                style="@style/ParameterButton"
                android:text="Param B" />
        </LinearLayout>
    
        <LinearLayout android:id="@+id/tableRow2"
            android:layout_below="@id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button android:id="@+id/buttonC"
                style="@style/ParameterButton"
                android:text="Param C" />
            <Button android:id="@+id/buttonD"
                style="@style/ParameterButton"
                android:text="Param D" />
        </LinearLayout>
    
        <LinearLayout android:id="@+id/tableRow3"
            android:layout_below="@id/tableRow2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button android:id="@+id/buttonE"
                style="@style/ParameterButton"
                android:text="Param E" />
            <Button android:id="@+id/buttonF"
                style="@style/ParameterButton"
                android:text="Param F" />
        </LinearLayout>
    
    
    
            <ListView android:id="@android:id/list"
                android:layout_below="@id/tableRow3"
                android:layout_toLeftOf="@+id/linearLayoutTotal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
    
            <TableLayout android:id="@+id/calcKeypad"
                android:layout_below="@id/tableRow3"
                android:layout_toLeftOf="@id/linearLayoutTotal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:stretchColumns="*">
    
                <TableRow
                    android:layout_weight="0.25"
                    android:gravity="center">
                    <Button android:id="@+id/button7"
                        style="@style/KeypadButton"
                        android:text="7" />
                    <Button android:id="@+id/button8" 
                        style="@style/KeypadButton"
                        android:text="8" />
                    <Button android:id="@+id/button9" 
                        style="@style/KeypadButton"
                        android:text="9" />
                </TableRow>
    
                <TableRow
                    android:layout_weight="0.25"
                    android:gravity="center">
                    <Button android:id="@+id/button4"
                        style="@style/KeypadButton"
                        android:text="4" />
                    <Button android:id="@+id/button5" 
                        style="@style/KeypadButton"
                        android:text="5" />
                    <Button android:id="@+id/button6"
                        style="@style/KeypadButton"
                        android:text="6" />
                </TableRow>
    
                <TableRow
                    android:layout_weight="0.25"
                    android:gravity="center">
                    <Button android:id="@+id/button1"
                        style="@style/KeypadButton"
                        android:text="1" />
                    <Button android:id="@+id/button2" 
                        style="@style/KeypadButton"
                        android:text="2" />
                    <Button android:id="@+id/button3" 
                        style="@style/KeypadButton"
                        android:text="3" />
                </TableRow>
    
                <TableRow
                    android:layout_weight="0.25"
                    android:gravity="center">
                    <Button android:id="@+id/buttonClear"
                        style="@style/KeypadButton"
                        android:text="C" />
                    <Button android:id="@+id/button0" 
                        style="@style/KeypadButton"
                        android:text="0" />
                    <Button android:id="@+id/buttonDecimal" 
                        style="@style/KeypadButton"
                        android:text="." />
                </TableRow>
            </TableLayout>
    
        <LinearLayout android:id="@id/linearLayoutTotal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_below="@id/tableRow3"
            android:layout_alignParentRight="true"
            android:orientation="vertical">
    
            <Button android:id="@+id/buttonTotalWeight"
                style="@style/FunctionButton"
                android:text="Function A" />
            <Button android:id="@+id/buttonTotalCost" 
                style="@style/FunctionButton"
                android:text="Function B" />
            <Button android:id="@+id/buttonAbout" 
                style="@style/FunctionButton"
                android:layout_height="0dp"
                android:layout_weight="0.25"
                android:text="Function C" />
        </LinearLayout>
    
    </RelativeLayout>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I am not going to post all of the code here but I
I was going to post this on Database Administrators, but as it is slightly
Going round in circles here i think. I have an activity called Locate; public
I have create an rss feed, all things is going to work well, but
I think the title explains it all but I am going deeper into my
This is going to be a long post and just for fun, so if
I post this topic because I have a problem with my iPhone application since
Pretty new to all this, so if I am going about my puzzle in
There are several questions throughout this post all related to the title. The overall
I'm a little embarrassed to post this but I can't seem to figure out

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.