Stretch one column, shrink others in Android TableLayot - android

Below I would like to strech the pool_team column and have the others shrink to the column width. I can't seem to get it right though. The other columns will contain an integer of small size and the pool_team will contain a long name that may wrap.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pool_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="0">
<TableRow
android:id="#+id/pool_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="#+id/pool_number"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textAlignment="center" />
<TextView
android:id="#+id/pool_team"
android:layout_width="0dip"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/pool_wins"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textAlignment="center" />
<TextView
android:id="#+id/pool_loses"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textAlignment="center" />
<TextView
android:id="#+id/pool_point_difference"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textAlignment="center" />
<TextView
android:id="#+id/pool_place"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textAlignment="center" />
</TableRow>
</TableLayout>

Something like this?
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pool_table"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/pool_row"
android:weightSum="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="#+id/pool_number"
android:layout_width="0dp"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="123" />
<TextView
android:id="#+id/pool_team"
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Some longer text that may or may not wrap around to the next line...." />
<TextView
android:id="#+id/pool_wins"
android:layout_weight=".1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="123" />
<TextView
android:id="#+id/pool_loses"
android:layout_weight=".1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="123" />
<TextView
android:id="#+id/pool_point_difference"
android:layout_width="0dp"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="123" />
<TextView
android:id="#+id/pool_place"
android:layout_weight=".1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="123" />
</TableRow>

Related

Prevent expansion of table column when contents imcrease in size, fixed width

as you can see, they arranged in a table layout, i set their layout weight to 1 and their weight sum to 4, at first they are all equal in width, but that changes when the nunber has a lot of digits, what to do?
i thought using a table layout was more convenient than multiple linear layouts haha
layout:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright"
android:weightSum="4">
<TextView
android:id="#+id/textView9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Food"
android:textSize="12sp" />
<TextView
android:id="#+id/textView10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Supplies"
android:textSize="12sp" />
<TextView
android:id="#+id/textView11"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Transport"
android:textSize="12sp" />
<TextView
android:id="#+id/textView12"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Misc"
android:textSize="12sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_weight="1">
<TextView
android:id="#+id/tvTotalExpenses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="18sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright"
android:weightSum="4">
<TextView
android:id="#+id/tvFoodExpense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="14sp" />
<TextView
android:id="#+id/tvSuppliesExpense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="14sp" />
<TextView
android:id="#+id/tvTransportExpense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="14sp" />
<TextView
android:id="#+id/tvMiscExpense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="14sp" />
</TableRow>
</TableLayout>
You are mentioning layout weight but it will not work properly unless you set the width to 0dp so set layout_width to 0dp like this
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright"
android:weightSum="4">
<TextView
android:id="#+id/textView9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Food"
android:textSize="12sp" />
<TextView
android:id="#+id/textView10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Supplies"
android:textSize="12sp" />
<TextView
android:id="#+id/textView11"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Transport"
android:textSize="12sp" />
<TextView
android:id="#+id/textView12"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Misc"
android:textSize="12sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:layout_weight="1">
<TextView
android:id="#+id/tvTotalExpenses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="18sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright"
android:weightSum="4">
<TextView
android:id="#+id/tvFoodExpense"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="14sp" />
<TextView
android:id="#+id/tvSuppliesExpense"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5000000"
android:gravity="center"
android:textSize="14sp" />
<TextView
android:id="#+id/tvTransportExpense"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="50000"
android:textSize="14sp" />
<TextView
android:id="#+id/tvMiscExpense"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textSize="14sp" />
</TableRow>
</TableLayout>

Different Layouts with same weights gets different size

In my layout I have two different LinearLayouts. The first one is this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8"
android:gravity="center"
android:text="#string/lblPais" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblJ" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblG" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblP" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblPF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblPC" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblDif" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblPuntos" />
</LinearLayout>
And the second one is this layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="15" >
<TextView
android:id="#+id/lblPais1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8"
android:gravity="center"
android:text="#string/lblPais" />
<TextView
android:id="#+id/lblJ1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lbl0" />
<TextView
android:id="#+id/lblG1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lbl0" />
<TextView
android:id="#+id/lblP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lbl0" />
<TextView
android:id="#+id/lblPF1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lbl0" />
<TextView
android:id="#+id/lblPC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lbl0" />
<TextView
android:id="#+id/lblDif1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblPtsVacio" />
<TextView
android:id="#+id/lblPuntos1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblPtsVacio" />
</LinearLayout>
As you can see both layouts get the same value for the layout_width attribute (match_parent). Inside both layouts I have the same number of elements (8 TextView). Each TextView has the same weight except the first one, that has a weight of 8.
If both layouts are the same (only differ the label on the TextViews) why do they have different sizes?
Try this way,hope this will help you to solve your problem.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:gravity="center"
android:text="#string/lblPais" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblJ" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblG" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblP" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblPF" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblPC" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblDif" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblPuntos" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/lblPais1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:gravity="center"
android:text="#string/lblPais" />
<TextView
android:id="#+id/lblJ1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lbl0" />
<TextView
android:id="#+id/lblG1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lbl0" />
<TextView
android:id="#+id/lblP1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lbl0" />
<TextView
android:id="#+id/lblPF1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lbl0" />
<TextView
android:id="#+id/lblPC"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lbl0" />
<TextView
android:id="#+id/lblDif1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblPtsVacio" />
<TextView
android:id="#+id/lblPuntos1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/lblPtsVacio" />
</LinearLayout>
</LinearLayout>
if you want all layout to be same as your weight
your orientation is vertical so change...
android:layout_height="wrap_content"
to
android:layout_height="0dp"
In all TextView
You havent given android:weightSum="15" in first linear layout.
You have to add andoid:weightSum="15" on the first parent layout and everything should fall into place.
Make your parent linear layout height as
android:layout_height="match_parent"

TableLayout doesn't fit the screen

I am a beginner and I have designed a layout that displays the rows from the XML upon the click of a "show" Button.
But when I don't click the show button, I want the table row with id-blank to increase its height to fit the screen.
Please suggest a way to do that.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Row 1 with single column -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:gravity="center_horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="Sale Report"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<!-- Row 2 with 3 columns -->
<LinearLayout
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#drawable/borders"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<EditText
android:id="#+id/starttime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:inputType="none"
android:textSize="#dimen/textSize" />
<EditText
android:id="#+id/stoptime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:inputType="none"
android:textSize="#dimen/textSize" />
<Button
android:id="#+id/show"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight=".75"
android:text="show" />
<Button
android:id="#+id/back"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight=".75"
android:text="back" />
</LinearLayout>
<!-- Row 3 with 2 columns -->
<TableLayout
android:id="#+id/sale_table_data"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="#drawable/borders"
android:gravity="center" >
<TableRow
android:id="#+id/sale_report_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/Yellow" >
<TextView
android:id="#+id/particulars"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="Particulars"
android:textStyle="bold" />
<TextView
android:id="#+id/voucherNo"
android:layout_weight="1"
android:gravity="fill_horizontal"
android:text="VCH No"
android:textStyle="bold" />
<TextView
android:id="#+id/quantity"
android:layout_weight="1"
android:gravity="fill_horizontal"
android:text="Quantity"
android:textStyle="bold" />
<TextView
android:id="#+id/amount"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="Amount"
android:textStyle="bold" />
<TextView
android:id="#+id/date"
android:layout_weight="1.3"
android:gravity="center_horizontal"
android:text="Date"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/blank"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3" >
<TextView
android:id="#+id/blank9"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="Particulars"
android:textStyle="bold" />
<TextView
android:id="#+id/blank10"
android:layout_weight="1"
android:gravity="fill_horizontal"
android:text="VCH No"
android:textStyle="bold" />
<TextView
android:id="#+id/blank11"
android:layout_weight="1"
android:gravity="fill_horizontal"
android:text="Quantity"
android:textStyle="bold" />
<TextView
android:id="#+id/blank12"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="Amount"
android:textStyle="bold" />
<TextView
android:id="#+id/blank13"
android:layout_weight="1.3"
android:gravity="center_horizontal"
android:text="Date"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/no_borders" >
<TextView
android:id="#+id/blank1"
android:layout_weight="1"
android:gravity="center_horizontal"
android:textStyle="bold" />
<TextView
android:id="#+id/blank2"
android:layout_weight="1"
android:gravity="center_horizontal"
android:textStyle="bold" />
<TextView
android:id="#+id/Total"
android:layout_weight="1"
android:gravity="center"
android:text="Total"
android:textStyle="bold" />
<TextView
android:id="#+id/total_sales"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="Amount_value"
android:textStyle="bold" />
<TextView
android:id="#+id/blank3"
android:layout_weight="1"
android:gravity="center_horizontal"
android:textStyle="bold" />
</TableRow>
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>

Table layout column spacing

I am trying to format my tablelayout but I dont seem to get things working quite well.
I have 4 columns that I need them spaced as nice as possible. I was thinking really having 3 columns with same width and the last column taking the rest of the space. The last column is ok to wrap around in 2 or more lines but the first 3 columns has to be single line and NOT truncated. Here is my code
<TableLayout android:id="#+id/tlMylayout" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:stretchColumns="*" android:shrinkColumns="*" >
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="#style/showtext_style" android:text="col1" />
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="#style/showtext_style" android:text="col2" />
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="#style/showtext_style" android:text="col3" />
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="#style/showtext_style" android:text="lastCol" />
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="2011-01-01 14:59" />
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:text="50" />
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:text="0.57" />
<TextView android:gravity="center" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is the last one and can be very long string if I want it to be" />
</TableRow>
</TableLayout>
Try
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tlMylayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:text="col1" />
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:text="col2" />
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:text="col3" />
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:text="lastCol" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:text="2011-01-01 14:59" />
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:singleLine="true"
android:text="50" />
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:singleLine="true"
android:text="0.57" />
<TextView
android:layout_width="0"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:text="this is the last one and can be very long string if I want it to be" />
</TableRow>
I removed layout_gravity and added layout_weight to TextView

Android: making a simple layout

I would like to construct a simple table layout in Android BUT I got some problems with it.
At part 1: there are some text views so I would like to put that TableRow's height to wrap_content.
At part 2: same thing.
It works all still now BUT:
At part 3: there are 2 listview-s and at part 4 there are some buttons.
I would like to do something like this:
let the part 4's height to wrap content but make the part 3 to fill all the empty space on the screen.
Please give me a solution for this, beacause when i probed the part 3 filled out all the emty space till the bottom of the screen. Now I tryed with weight_sum BUT I do not like it because I would let the 4'd row to wrap_content.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:weightSum="1.0" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:weightSum="1.0" >
<TextView
android:id="#+id/phone_tab_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:background="#layout/style_right_border"
android:gravity="center"
android:singleLine="true"
android:text="Phone Tab"
android:textColor="#android:color/white"
android:textSize="14dp" />
<TextView
android:id="#+id/pc_tab_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:gravity="center"
android:singleLine="true"
android:text="PC Tab"
android:textColor="#android:color/white"
android:textSize="14dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#layout/style_bottom_border" >
<TextView
android:id="#+id/phone_path_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:background="#layout/style_bott_right_border"
android:gravity="center"
android:paddingBottom="5dp"
android:singleLine="true"
android:text="mnt/sdcard/Movies/dpad/sadas/asda/sda/as"
android:textColor="#android:color/white"
android:textSize="8dp" />
<TextView
android:id="#+id/pc_path_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:gravity="center"
android:paddingBottom="5dp"
android:singleLine="true"
android:text="mnt/sdcard/Movies/dpad/sadas/asda/sda/as"
android:textColor="#android:color/white"
android:textSize="8dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="0.9"
android:weightSum="1.0" >
<ListView
android:id="#+id/listview_phone_files"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:background="#layout/style_right_border" />
<ListView
android:id="#+id/listview_pc_files"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="0.1"
android:background="#layout/style_top_border"
android:gravity="center"
android:weightSum="1.0" >
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Button" />
<ImageButton
android:id="#+id/button_active_tab_changer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:background="#android:color/transparent"
android:onClick="changeActiveTabButton"
android:src="#drawable/button_change_tab" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Button" />
</TableRow>
Create a RelativeLayout as parent and place the TableLayouts inside that. That allows you to use alignParentBottom and alignParentTop layaout params.
check it this layout, but please keep your old layout file in backup.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:weightSum="1.0" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:weightSum="1.0" >
<TextView
android:id="#+id/phone_tab_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="center"
android:singleLine="true"
android:text="Phone Tab"
android:textColor="#android:color/white"
android:textSize="14dp" />
<TextView
android:id="#+id/pc_tab_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:gravity="center"
android:singleLine="true"
android:text="PC Tab"
android:textColor="#android:color/white"
android:textSize="14dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/phone_path_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="center"
android:paddingBottom="5dp"
android:singleLine="true"
android:text="mnt/sdcard/Movies/dpad/sadas/asda/sda/as"
android:textColor="#android:color/white"
android:textSize="8dp" />
<TextView
android:id="#+id/pc_path_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:gravity="center"
android:paddingBottom="5dp"
android:singleLine="true"
android:text="mnt/sdcard/Movies/dpad/sadas/asda/sda/as"
android:textColor="#android:color/white"
android:textSize="8dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
<ListView
android:id="#+id/listview_phone_files"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<ListView
android:id="#+id/listview_pc_files"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="0.1"
android:gravity="center"
android:weightSum="1.0" >
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Button" />
<ImageButton
android:id="#+id/button_active_tab_changer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:background="#android:color/transparent"
android:onClick="changeActiveTabButton"
android:src="#android:drawable/ic_delete" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Button" />
</TableRow>
</TableLayout>
set tableRow3's android:layout_height to 0, and android:layout_weight to 1. The other tableRows can use android:layout_height="wrap_content".

Categories

Resources