0

This is my layout:

TEXTVIEW

IMAGEVIEW (optional)

LINEARLAYOUT - to which I add Buttons dynamically

LINEARLAYOUT - with two buttons side by side (left button and right button)

What do I need to do to ensure that the bottom two linear layouts are fixed to the bottom of the screen, regardless of how much space they may take up? ie. The first linear layout might have 3 buttons and take up over half the screen, which is okay. It just needs to be above the left/right buttons in the last linear layout, which is fixed to the bottom.

Then I want my TextView and my ImageView vertically centred in the remaining space. The ImageView will be set to invisible if there is no image, so it could only be the text view which needs to be centred.

I've been playing around with android:gravity="bottom", android:layout_height="0dip"/android:layout_weight="1" (I later realised this would only give 50% to the text/imageview and 50% to the 2 linear layouts), but I can't get my desired result.

Any advice appreciated.

b85411
  • 9,420
  • 15
  • 65
  • 119
  • 1
    show us your exact code.. – Anirudha Jan 23 '14 at 07:09
  • 1
    *What do I need to do to ensure that the bottom two linear layouts are fixed to the bottom of the screen* - wrap all the views in a `RelativeLayout`, the `LinearLayout` with the two buttons will have the rule `layout_alignParentBottom`, the other `LinearLayout` will be above the first `LinearLayout`(`layout_above`). Wrap the other remaining two views in another layout and place that between the top margin and above the dynamic `LinearLayout`. – user Jan 23 '14 at 07:11
  • Hi Luksprog, I have tried this (and what owe suggested) and it's close but what I've found is that the text view at the top is, for some reason, top aligned and left aligned no matter what. Ideally, I'd like the text/image to be center aligned together, but if there is no image (ie. Visibility.GONE) then the text is still center aligned. The code I'm using is this: http://pastebin.ca/2580989 – b85411 Jan 24 '14 at 03:20

1 Answers1

1

You have to take RelativeLayout. There you have a better control of the relative position of the views, something like this:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
>
    <TextView 
        android:id="@+id/textView"
        android:layout_above="@+id/imageView"
        android:layout_centerVertical="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

    <ImageView 
        android:id="@+id/imageView"
        android:layout_above="@+id/ll_1"
        android:layout_centerVertical="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

    <LinearLyout 
        android:id="@+id/ll_1"
        android:layout_above="@+id/ll_2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

    <LinearLyout 
        android:id="@+id/ll_2"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />


</RelativeLayout>
owe
  • 4,890
  • 7
  • 36
  • 47
  • Hi owe, I have followed your example (and what Luksprog said) and it's close but not quite there. I've explained what is happening in reply to Luksprog's answer. Thanks. – b85411 Jan 24 '14 at 03:22
  • Try this: change your `layout_width` and `layout_height` attribute of the `LinearLayout` to `wrap_content`. Also change `android:layout_centerVertical="true"` to `android:layout_centerInParent="true"`. And here is a post of how to use `gravity` attribute for the views content: http://stackoverflow.com/questions/13965883/what-is-exact-difference-between-gravity-and-layout-gravity – owe Jan 25 '14 at 22:32