Android Layout LinearLayoutProblem - android

I want to have a site, which contains of a vertical layout, that has a content part and a footer part. The footer part should be 20% of the height of the display height. I think that it is a very common usecase.
I have tried some ways, but was not successful in the end.
How would i implement this?
TU in advance!

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="-1">
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="20"/>
<LinearLayout
android:id="#+id/body"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="60"/>
<LinearLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="20"/>
</LinearLayout>
This should give you a header and footer that take 20% of the space, with the body occupying 60%. See http://developer.android.com/reference/android/widget/LinearLayout.html#setWeightSum(float) for a simple description of what this is doing.

This is what worked for me to display a footer with ads.
<LinearLayout>
<LinearLayout xmlns:myapp="http://schemas.android.com/apk/res/com.broschb.wiiscale"
android:layout_width="fill_parent" android:layout_marginTop="5px"
android:layout_below="#+id/weight" android:layout_height="fill_parent"
android:gravity="center_horizontal|bottom">
<myFooterComponents android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>

LinearLayout, right? Just set the height of both your content and footer layouts to fill_parent, and give the content area a layout_weight of 8, and the footer a layout_weight of 2.

Related

Using RelativeLayout to make a View take all remaining space

I want to achieve this
I am doing this
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/upper"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/wallet_dim_foreground_inverse_disabled_holo_dark">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_below="#+id/upper"
android:background="#color/blueAccentColor">
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/upper"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/lower"
android:background="#color/wallet_dim_foreground_inverse_disabled_holo_dark">
</LinearLayout>
<LinearLayout
android:id="#+id/lower"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:background="#color/blueAccentColor"></LinearLayout>
</RelativeLayout>
You have to set the first Layout to be above the seconde.
With the code you provided you will have a top layout taking all space, and a bottom layout being below him. If you think about it you can see that it's logic that the result will be different.
If you remove android:layout_below="#+id/upper"and add android:layout_alignParentBottom="true"from the lower one, this Layout will be anchored to the bottom side, whichever the screen size is.
Than adding android:layout_above="#+id/lower" to the top Layout, he will take all space(thanks to android:layout_height="fill_parent") but it will stay above the lower Layout.
Here is the result you want :)

How to stick a view at the bottom of the screen so the above part is movable?

My layout consists of a ScrollView which holds everything. At some point I need to show a View at the bottom of the screen on top (above) of ScrollView (so ScrollView goes behind the View) so I could scroll the screen up and down while this View still be sticked to a device's screen bottom. Please advice guys how to do such a layout. Thank you.
If you want the scrollview to go behind the view, this can be done like this:
(partial code copy from this SO question)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/RelativeLayout1">
<ScrollView android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ScrollView>
<View android:id="#+id/bottomView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="gone">>
</View>
</RelativeLayout>
The scrollview will now expand beyond the View element.
You could create two fragments and use the weight attribute to distribute the space. The fragment at the top would host you root scrolView and the fragment at the bottom would have the View.
Okay two options, first one:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<View
android:id="#+id/myView"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/myView" />
</RelativeLayout>
This way your ScrollView will always be above of your View. But it seems like you want the ScrollView to fill the whole screen while the View "hides" some of the items of your SV. So check this code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<View
android:id="#+id/myView"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>

Android <include> ListView not taking up its height

I have a very strange problem here. In the following xml section I'm trying to use include:
<!-- CONTENT -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdge="none">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include android:id="#+id/summary"
layout="#layout/view_summary"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</ScrollView>
(the section is in a main linear layout with vertical orientation and both width and height set to fill parent)
The included xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white">
<ListView android:id="#+id/news_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:cacheColorHint="#color/transparent"
android:background="#color/transparent" />
<Button android:id="#+id/news_load_more"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:text="Load more..."/>
</LinearLayout>
The problem is that the ListView is not taking up the whole height (the height of the screen), only the size of one single list item. I have tried setting layout parameters from code, but it didn't helped.
Any idea is welcomed.
Thanks.
Don't put listview inside scrollview. Remove the scrollview.
Add android:layout_weight="0" to <Button android:id="#+id/news_load_more" ... /> This will tell it that <ListView android:id="#+id/news_list" ...> is the one to be expanded to fill its parent while the button must not.
Please changed android:layout_height="wrap_content" instead of android:layout_height="fill_parent"
only not changed ScrollView height property .
may be help to you.

Using layout_weight with ImageView in LinearLayout

I am trying to place an ImageView in the middle of the screen, with the aim of putting other views above and below it so I have decided to use a LinearLayout (vertical) and a LinearLayout (horizontal). However, when I use layout_weight for the ImageView, the image disappears completely (a blank screen is shown). The ImageView's image is set in onCreate(). I would like the ImageView to take up 50% of the width of the screen, hence my use of weights.
<?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">
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_weight="2">
<View android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<ImageView android:layout_weight="2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/albumArtImageView"></ImageView>
<View android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
For layout_weight to be useful for anything, there needs to be left-over space. Your first view under the top LinearLayout has layout_height="fill_parent". That takes up all the room. Try setting layout_height="0dp" everywhere that you want the dimension determined by the layout_weight and I think your results will be better.
try to change the orientation of second linearlayout to vertical

Buttons not displaying in linear layout

Hi all
my need is that buttons must show at bottom, so my code as
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="12dip">
<ListView
android:id="#+id/list_multiselectable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
</ListView>
<LinearLayout android:orientation="horizontal"
android:background="#android:drawable/bottom_bar"
android:paddingLeft="4.0dip" android:paddingTop="5.0dip"
android:paddingRight="4.0dip" android:paddingBottom="1.0dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
my buttons code here
</LinearLayout>
</LinearLayout>
But when items in list are less (until scroll bar of list view is not enable), Buttons are visible, but when items in list are more, buttons are not visible, although I scrolled list item to last row, but at last buttons are not displaying. How can I solve it.
Thank you!
LinearLayout is giving ListView all the space it needs, because it considers its children sequentially and doesn't take into account those that follow.
My suggestion is to use RelativeLayout.
<RelativeLayout ...>
<ListView ...
android:id="#+id/list"
android:layout_alignParentTop="true"/>
<LinearLayout ...
android:layout_below="#id/list"
android:layout_alignParentBottom="true">
...buttons...
</LinearLayout>
</RelativeLayout>
I am not sure. But you may try to set the weight of your listview.
<ListView
android:id="#+id/list_multiselectable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical">
</ListView>
You can set the minimumheight of listview
e.g. 320*480
listview height = 430
button's layout height = 50

Categories

Resources