How to set height according to the highest column? - android

I use tableLayout, and I want to set height of tableRow as tall as it has to be in order to display textViews correctly (which are in linearLayout). At the moment, the second textView is not displayed correctly (it simply doesn't fit in the row).
This is the XML:
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/background">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/stripe_background"
android:gravity="right|center_vertical"
android:paddingTop="#dimen/stripe_padding_buttons_top_bottom"
android:paddingBottom="#dimen/stripe_padding_buttons_top_bottom"
android:paddingLeft="#dimen/stripe_padding_sides"
android:paddingRight="#dimen/stripe_padding_sides"
android:layout_marginBottom="#dimen/stripe_margin_top_bottom">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_column="0"
android:layout_weight="1"
android:paddingRight="#dimen/stripe_padding_between_text_views"
android:gravity="right|center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/settings_text_view_current_consumption_main"
android:id="#+id/textViewCurrentConsumptionMain"
android:textColor="#color/big_text"
android:textSize="#dimen/small_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/settings_text_view_current_consumption_additional"
android:id="#+id/textViewCurrentConsumptionAdditional"
android:textColor="#color/small_text"/>
</LinearLayout>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkBox"
android:layout_column="1"
android:button="#drawable/check_box_custom"
android:checked="true" />
</TableRow>
</TableLayout>
In order to make my question clearer, this is the problem in the image. Second textView is not displayed properly:

Make LinearLayout and all TextViews heights to wrap_content (TableRow) also

Related

Show 3 scrollable ListViews of equal height in Android Layout

I am trying to show three different vertical sections in my Android layout on one screen, each taking up one third of the screen, one on the top, one in the middle, and one on the bottom. Each section has a TextView and a ListView, and the ListViews can scroll so that you can see all items, but the overall page does not move. I have tried putting each TextView and ListView in a LinearLayout and setting the height of each LinearLayout to one third the total height of the screen, but the first ListView just shows all the items in it, taking up most of the screen, and the other sections are pushed down. I also tried using layout_weights, but for some reason it did not work. (EDIT: Setting layout_weight="1" ended up working, I'm not sure what I did wrong the first time) How can I make this work, or is there a better way to go about this?
<LinearLayout
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#FF0000"/>
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#00FF00">
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#0000FF">
</LinearLayout>
This will give you three equally wide columns: to make it rows, change the orientation to vertical and swap the values of layout_height and layout_width for each listview. If you need to add the TextViews, you'll have to make each listview in this code either a RelativeLayout LinearLayout or FrameLayout, using same width/height/weight and arranging the ListView and TextView inside to taste. To do this most efficiently, use a Framelayout and use a margin on the listview to offset it from the TextView. You can place the TextView relative to the ListView inside the FrameLayout by using the layout_gravity in the TextView.
Ie (swapping the first "column"):
<FrameLayout android:orientation="vertical" android:layout_width="0dp"
android:layout_weight="1" android:layout_height="match_parent"
android:background="#FF0000">
<TextView android:text="Column!" android:background="#3Eff0000"
android:layout_height="40dp" android:layout_width="match_parent">
<ListView android:layout_marginTop="48dp" android:layout_height="match_parent"
android:layout_width="match_parent" android:background="#8Aff0000"/>
</FrameLayout>
Use this:
<?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="horizontal"
android:weightSum="3">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ff89ff91">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#1"
android:id="#+id/textView"
android:padding="5dp"
android:gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/textView" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffff8177">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#2"
android:id="#+id/textView2"
android:padding="5dp"
android:gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView2"
android:layout_below="#+id/textView2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffe85d">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#3"
android:id="#+id/textView3"
android:padding="5dp"
android:gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView3"
android:layout_below="#+id/textView3" />
</RelativeLayout>
</LinearLayout>

Evenly fit ListViews and a Button into a LinearLayout

I want to fit two Listviews (+two Textviews) and a Button evenly into a LinearLayout. The final result should look something like this:
The Listviews can contain any number of items (from empty to requiring to scroll), however the button must be always visible. A lot of answers here suggest to use layout_weight along with layout_height="0dp" and layout_width="fill_parent", however this results in the Listviews taking up all available space and pushing the button out of the screen. I assume the parameters for the button are wrong, but I can't figure out which of them. At least the TextViews are working, since they are positioned correctly in every case.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="left"/>
<ListView
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginTop="12dp"
android:gravity="left"/>
<ListView
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"/>
<Button
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"/>
</LinearLayout>
I want to avoid to further nest the layout with additional LinearLayouts, unless there is no other way.
Something like this (not tested):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"/>
<ListView
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginTop="12dp"/>
<ListView
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="fill_parent"/>
<Button
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="wrap_content"/>
</LinearLayout>
Your button needs to have wrap_content height and (I think the reason of your bug) LinearLayout should have fill_parent as the height.

LinearLayout with gravity and weights

In my LinearLayout the use of weight in Combination with Gravity causes to reverse the effect of weight. The Layout:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="0.2">
<TextView android:id="#+id/feedfragmentTitle" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="8"/>
<TextView android:id="#+id/feedfragmentDate" android:layout_height="wrap_content" android:gravity="right" android:layout_width="fill_parent" android:layout_weight="2"/>
</LinearLayout>
The given LinearLayout contains a Title, which should appromately take 80% of the Layout and an Date+Time with 20%. The Date+Time should have the Gravity of right. The layout i posted doesnt work unfortunately and if i play with the parameters weight, the result is the inversed one.
Is there another way to get the Results without swapping the weights ?
When you use the layout_weight property you need to make the appropriate height or width as 0dip
try this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/feedfragmentTitle"
android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="8" android:text="sdfdfsfsd"/>
<TextView android:text="aass" android:id="#+id/feedfragmentDate"
android:layout_height="wrap_content"
android:gravity="right" android:layout_width="0dip"
android:layout_weight="2"/>
</LinearLayout>
Try like this
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="#+id/feedfragmentTitle" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="left"/>
<TextView android:id="#+id/feedfragmentDate" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_width="wrap_content" />
</LinearLayout>

How can I layout two buttons to be the same size?

The layout definition is below. Basically I want the two buttons to be the same width, but right now their width is determined by the string they display, is there a better way to lay this out such that the buttons are the same width and that the Button+EditText combo fills the width of the screen?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/editorlinearlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/setNameBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Set Item Name "/>
<EditText android:id="#+id/nameTxtBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/setGroupBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Group Name"/>
<EditText android:id="#+id/groupTxtBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Edit:
I've tried using a TableLayout as was suggested below, but now the EditText's don't fill the remaining portion of the screen. See the image and layout below.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<Button android:id="#+id/setNameBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Set Item Name "/>
<EditText android:id="#+id/nameTxtBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<Button android:id="#+id/setGroupBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Group Name"/>
<EditText android:id="#+id/groupTxtBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
the trick is to combine the tableview defined in the edit above with a strechColumn tag:
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow>
<Button android:id="#+id/setNameBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Set Item Name "/>
<EditText android:id="#+id/nameTxtBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
Use a TableLayout and TableRows.
Three options:
Put this in a TableLayout instead of the nested LinearLayout thing you are currently doing
Change your nesting from a vertical parent with horizontal children to a horizontal parent with vertical children (however, this opens up possible vertical positioning issues, so I don't favor this option)
Use a RelativeLayout and all of the layout_* attributes that RelativeLayout children have available to them
I'd favor option 1, personally.
you can use specific width and height for laout_width/layout_height, as in layout_width="150dip". You run the risk of having a button that can't fit all your text though, but if your text is fixed, you can double check to make sure it's all good.

Android layout with 2 evenly spaced Buttons

I have this layout that works correctly, a relative layout with a text view and two buttons spaced evenly below it.
<RelativeLayout android:id="#+id/entrypopup"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="5px"
android:visibility="gone"
android:layout_below="#+id/ad"
android:background="#80000000">
<TextView android:id="#+id/title" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Entry Popup..."
android:textColor="#ffffffff"
android:textSize="20sp" />
<TableLayout android:id="#+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title">
<TableRow android:layout_weight="1">
<Button android:id="#+id/buttonVisit" android:text="View"
android:layout_height="fill_parent"
android:layout_width="0dip"
android:layout_weight="1"/>
<Button android:id="#+id/buttonCancel" android:text="Cancel"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent"/>
</TableRow>
</TableLayout>
</RelativeLayout>
But running layoutopt it says that "this TableRow layout or its TableLayout parent is possible useless".
Is there a way to do this layout then without the tables?
Dump the TableLayout and TableRow, and just use a horizontal LinearLayout in their place. The "magic" is in your 0dip width and 1 weight, which you already have.

Categories

Resources