I want to make a re-usable table layout, in which I have 3 rows and last row contains 1 element only whereas first two contains 2 element each.
Now, problem is, rows in the table layout are not of equal height despite of giving equal weight to each row.
Bottom row is consuming too much space.
Following is the layout content:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="2" >
<TextView
android:id="#+id/txtvw_age"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/age" >
</TextView>
<TextView
android:id="#+id/txtvw_roll_no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/roll_no" >
</TextView>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="2" >
<TextView
android:id="#+id/txtvw_standard"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/standard" >
</TextView>
<TextView
android:id="#+id/txtvw_section"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/section" >
</TextView>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<TextView
android:id="#+id/txtvw_ct_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/ct_name" >
</TextView>
</TableRow>
</TableLayout>
try this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="2" >
<TextView
android:id="#+id/txtvw_age"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/save" >
</TextView>
<TextView
android:id="#+id/txtvw_roll_no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/save" >
</TextView>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="2" >
<TextView
android:id="#+id/txtvw_standard"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/save" >
</TextView>
<TextView
android:id="#+id/txtvw_section"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/save" >
</TextView>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/txtvw_ct_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/save" >
</TextView>
</TableRow>
</TableLayout>
I made the following change to the last TableRow and all goes well
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="1"
>
<TextView
android:id="#+id/txtvw_ct_name"
android:layout_width="0dp" // this t.v will consume all width
android:layout_weight="1"
android:layout_height="match_parent" // consume all height as of parent which is adjusted by weight.
android:text="#string/save"
>
</TextView>
</TableRow>
Since table layout has weight sum 3, i provide equal weight to the table row ( w.r.t height).
Related
How can I adjust the width of my 2 Relative Layouts correctly, so that the one to the left takes 0.25 of screen space and the one to the right takes 0.75? Inside the second relative layout there's a table layout with 3 columns, each of them should take 1/3 of table's size.
Here's what I've tried with AXML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.25">
<LinearLayout
android:id="#+id/buttonsLeft"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/buttonBack"
android:text="#string/btnBack"
android:layout_width="#dimen/myBtnWidth"
android:layout_height="#dimen/myBtnHeight"
android:layout_marginTop="#dimen/myBtnMargin"
android:layout_marginLeft="#dimen/myBtnMargin" />
<Button
android:id="#+id/buttonStart"
android:text="#string/btnStart"
android:layout_width="#dimen/myBtnWidth"
android:layout_height="#dimen/myBtnHeight"
android:layout_marginTop="#dimen/myBtnMargin"
android:layout_marginLeft="#dimen/myBtnMargin" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.75">
<LinearLayout
android:id="#+id/tableRight"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tableLayout1">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<TextView
android:id="#+id/Text1"
android:padding="10sp"
android:textSize="#dimen/myTextSizeColHeader"
android:text="#string/bin"
android:layout_height="wrap_content"
android:layout_width="#dimen/myColWidthBin"
android:gravity="left" />
<TextView
android:id="#+id/Text2"
android:padding="10sp"
android:textSize="#dimen/myTextSizeColHeader"
android:text="#string/item_customer"
android:layout_height="wrap_content"
android:layout_width="#dimen/myColWidthItemCust"
android:gravity="center" />
<TextView
android:id="#+id/Text4"
android:padding="10sp"
android:textSize="#dimen/myTextSizeColHeader"
android:text="#string/nominal_actualQty"
android:layout_height="wrap_content"
android:layout_width="#dimen/myColWidthActNomQty"
android:gravity="right" />
</TableRow>
<ListView
android:id="#+id/whseActivHeadersList" />
</TableLayout>
</LinearLayout>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="52.5dp"
android:id="#+id/notificationsNextorder"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
Result:
Add weightSum to parent linear layout.
Check this answer
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="4">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3">
</RelativeLayout>
Why do not the TableLayout and LinearLayout weights mean the same? Or do I have an error in the layout XMLs?
4 button test
I've prepared a simple test with 4 buttons having weights 1,1,1,3. The result is not the same using TableLayout (one column) and LinearLayout (vertical).
On the following link you can see a screenshot of both TableLayout (left) and LinearLayout (right) implementation.
http://i.stack.imgur.com/9dYlB.png
In my opinion the LinearLayout is correct - sum is 6, so the fourth button with weight 3 should be taking half the space.
TableLayout XML
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="A" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="B" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="D" />
</TableRow>
</TableLayout>
LinearLayout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="A" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="B" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="C" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:text="D" />
</LinearLayout>
Environment
ADT: 22.6.2
AVD: Intel Atom with 4.2.2 and 4.4.2 (both the same result)
It seems that if you change your weight value of the final button to 7 you'll achieve the same result as your second xml file. I think this is because it's accounting for the extra weight values you've put in the file by attributing both the tablerows and the buttons with weight. Here's what I used:
Edit: to be more clear, you have a weightsum (total) of 14 with this edited version, the final button has 7, which is half of the total. In your button-only layout you have a different weightsum.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="A" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="B" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="D" />
</TableRow>
I am using android:weight attribute.
First state
http://s1.postimg.org/umygk8han/first.png
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="223"
android:orientation="horizontal" >
</LinearLayout>
When add TextView
http://s7.postimg.org/89qjb42dn/two.png
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="223"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Sample"
android:textColor="#android:color/background_dark" />
</LinearLayout>
How can I solve this problem?
Best regards
I think that initially you need a RelativeLayout outside of the
LinearLayout , because I don't think it is a good idea to
assign layout_weight to a LinearLayout.
So do it like this:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="223"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Sample"
android:maxLines="1"
android:textColor="#android:color/background_dark" />
</RelativeLayout>
PS: I set the android:layout_height="0dp" of this RelativeLayout because it looked to me (according to the screenshots) that it was a node of a LinearLayour with orientation='vertical'
I understand that the linearlayout expands due to font size in the textView, try to decrease font size.
<TextView ....
android:textSize="9sp" />
Also you could try to set padding textview to 0
android:padding="0dp"
Hope it helps!
try setting android:layout_height="match_parent" in your TextView to android:layout_height="wrap_content"
Please try to use bellow code.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dip"
android:weightSum="2" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#android:color/white"
android:text="Sample" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dip"
android:weightSum="2" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#android:color/white"
android:text="Sample"
android:textColor="#android:color/background_dark" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dip"
android:weightSum="2" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#android:color/white"
android:text="Sample"
android:textColor="#android:color/background_dark" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dip"
android:weightSum="2" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#android:color/white"
android:text="Sample"
android:textColor="#android:color/background_dark" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dip"
android:weightSum="2" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#android:color/white"
android:text="Sample" />
</LinearLayout>
Why doesn't my table have equal column widths? Here's the complete layout: first the table, then two buttons in a LinearLayout outside of table, those are divided equally.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp" >
<TableRow
android:layout_marginTop="2dp"
android:gravity="center_vertical" >
<TextView
style="#style/Label.Plain"
android:layout_width="match_parent"
android:text="#string/Caption" />
<EditText
android:background="#00ff00"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="#dimen/button_height"
android:inputType="numberDecimal"
android:selectAllOnFocus="true"
android:textSize="#dimen/spinner_text" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="3dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<Button
android:id="#+id/btnOk"
style="#style/Button.Plain.Fill"
android:layout_weight="1"
android:text="#string/ALS_OK" />
<Button
android:id="#+id/btnClose"
style="#style/Button.Plain.Fill"
android:layout_weight="1"
android:text="#string/CANCEL" />
</LinearLayout>
</LinearLayout>
</ScrollView>
And here's how it looks (one column painted for visibility):
I could use linear layout here, just want to understand the logic behind TableLayout.
you can acquire this in different ways,
like using
android:stretchColumns="*"
in TableLayout or
android:weightSum="1"
in TableRow and
android:layout_width="0dp"
android:layout_height="wrap_content"
in and any views in TableRow.
here is an example
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="#+id/tblTop10List"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_marginTop="23dp"
android:stretchColumns="*" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<TextView
android:id="#+id/tvTopName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="HELLO THERE"
android:textColor="#FFF000"
android:layout_column="0"
/>
<TextView
android:id="#+id/tvTopNumber"
android:paddingLeft="4dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#FFF000"
android:text="HELLO THERE "
android:layout_column="1"
/>
</TableRow>
</TableLayout>
</LinearLayout>
** for more info about table layout and it's property please check this nice article
http://www.informit.com/articles/article.aspx?p=2007353&seqNum=7
I would like to create in my application statistics from a'la tiles metro. Unfortunately TextView hasn't equal width, as it solved?
This is my layout
Use android:weightSum and android:layout_weight
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="4" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="2"
android:background="#B0B0B0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:layout_weight="2"
android:background="#B0B0B0" />
</TableRow>
</TableLayout>