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.
Related
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
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.
this is my fifth day of Android development so be gentle!
I am trying to place two rows of three buttons into my SlidingDrawer, this is what I have so far but I can't understand why the second row of buttons are not visible?
<SlidingDrawer
android:id="#+id/SlidingDrawer"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:content="#+id/drawerButtons"
android:handle="#+id/slideHandleButton" >
<Button
android:id="#+id/slideHandleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/closearrow" />
<RelativeLayout
android:id="#+id/drawerButtons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#80000000" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Test1" >
</Button>
<Button
android:id="#+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Test2" >
</Button>
<Button
android:id="#+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Test3" >
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/Button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Test4" >
</Button>
<Button
android:id="#+id/Button05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Test5" >
</Button>
<Button
android:id="#+id/Button06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Test6" >
</Button>
</LinearLayout>
</RelativeLayout>
</SlidingDrawer>
The first row of buttons displays as expected but I don't see the second row? Just empty space.
Any help appreciated
You have dropped both LinearLayouts inside a RelativeLayout without giving the layout any hints how it should place its elements. Therefore by default both LinearLayouts will be rendered on top of each other at (0,0) inside the RelativeLayout.
One solution would be to give the top LinearLayout an id
android:id="#+id/topRow"
And then give the LinearLayout a hint where to place itself inside the RelativeLayout
android:layout_below="#id/topRow"
In addition to that you have to set layout_height of both LinearLayouts to wrap_content. Otherwise the first LinearLayout still fills the whole RelativeLayout and the other one is placed below outside of the screen.
Other solutions: Wrap the LinearLayouts inside a LinearLayout with orientation vertical or use a GridLayout (>= API level 14). You could also try to reduce the view tree and use just one RelativeLayout and use layout_below, layout_leftOf, ... to place the elements inside the layout.
try this below code :
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/drawerButtons"
android:orientation="vertical"
<LinearLayout>
// your buttons
</LinearLayout>
<LinearLayout>
// your buttons
</LinearLayout>
</LinearLayout>
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>
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.