23

I am trying to define a layout where an ImageView is aligned at the bottom of the screen and also under a GridView, to avoid overlapping.

This however results in the ImageView being set just under the GridView but not aligned with the bottom of the screen.

Can this be solved using a different kind of Layout?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/background"
    android:orientation="vertical">

    <RelativeLayout 
        android:id="@+id/gs_top_text_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:paddingBottom="30dp">

        <RelativeLayout 
            android:id="@+id/gs_top_sub_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:paddingBottom="30dp">

            <TextView
                android:id="@+id/text_l"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"/>

            <TextView
                android:id="@+id/text_t"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"/>

        </RelativeLayout>

        <RelativeLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/gs_top_sub_container"
            android:layout_centerHorizontal="true">

            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/text1"/>

        </RelativeLayout>



    </RelativeLayout>



    <GridView
        android:id="@+id/gs_grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/gs_top_text_container"
        android:descendantFocusability="blocksDescendants"
        android:horizontalSpacing="2dp"
        android:verticalSpacing="2dp"
        android:numColumns="3"
        android:gravity="center"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />


    <ImageView
        android:id="@+id/gs_bottom_logo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/gs_grid_view"
        android:layout_alignParentBottom="true"
        android:adjustViewBounds="true"
        android:maxWidth="200dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"/>

</RelativeLayout>
liarspocker
  • 2,434
  • 3
  • 17
  • 26

2 Answers2

26

This is how I would do that:

1 - Put the ImageView on the Bottom
2 - Put the GridView ABOVE it.

<!-- First anchor this View to the bottom -->
<ImageView
    android:id="@+id/gs_bottom_logo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:adjustViewBounds="true"
    android:maxWidth="200dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
/>
<!-- Then put this View ABOVE the ImageView -->
<GridView
    android:id="@+id/gs_grid_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/gs_top_text_container"
    android:layout_above="@id/gs_bottom_logo"
    android:descendantFocusability="blocksDescendants"
    android:horizontalSpacing="2dp"
    android:verticalSpacing="2dp"
    android:numColumns="3"
    android:gravity="center"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
/>

This way I used the relativity of elements in a RelativeLayout

[EDIT]

Just for fun... I optimized your WHOLE LAYOUT.
Please note how the same design is achieved by using many layouts less.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/background"
    >
    <TextView
        android:id="@+id/text_l"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingBottom="30dp"
    />
    <TextView
        android:id="@+id/text_t"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:paddingBottom="30dp"
    />
    <!--
        This container is (unfortunately) still needed, to make these two
        textViews horizontally centered... :(
    -->
    <RelativeLayout
        android:id="@+id/rlCentering"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_l"
        android:layout_centerHorizontal="true"
        android:paddingBottom="30dp"
        >
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/text1"
        />
    </RelativeLayout>

    <!-- First, anchor this View to the bottom -->
    <ImageView
        android:id="@+id/gs_bottom_logo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:adjustViewBounds="true"
        android:maxWidth="200dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
    />
    <!-- Then put this View ABOVE the ImageView -->
    <GridView
        android:id="@+id/gs_grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/rlCentering"
        android:layout_above="@id/gs_bottom_logo"
        android:descendantFocusability="blocksDescendants"
        android:horizontalSpacing="2dp"
        android:verticalSpacing="2dp"
        android:numColumns="3"
        android:gravity="center"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
    />
</RelativeLayout>

And this is what I got in the Graphical Editor (I put some images I had and some texts to identify the TextViews):

enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Although I didn't have time to test it, looks good. Thanks. Just try not being so rude next time you write a comment. – liarspocker Jun 03 '14 at 10:48
  • 2
    Was it rude? Sorry for that. I just wanted you to focus on practical vs best practices. After all, sometimes teachers shout. But they just want you to learn from your mistakes. Please, make a treasure of that - simplify as much as you can, flatten your designs as much as possible. And get the best results in performances without sacrifying the look. ;) - You may notice I **HAD TO** keep an inner RelativeLayout, just to horizontally center those 2 TextViews (you might maybe find a better alternative to eliminate the need for that container). – Phantômaxx Jun 03 '14 at 10:53
  • Just tried the code and it's perfect. The grid occupies all available space now, which looks a lot better. By the way, do you know how I can set up the GridView so that the items are evenly distributed? Right now, I end up with a lot of whitespace at the bottom when setting `android:stretchMode="spacingWidthUniform"`. Thanks – liarspocker Jun 03 '14 at 17:36
  • I'm glad you followed my advice and that it worked perfectly ;). Maybe this post http://stackoverflow.com/questions/7705330/gridview-and-excess-space-padding answers your new question? – Phantômaxx Jun 03 '14 at 17:56
  • Didn't work. I think I'll try to set the item sizes programatically – liarspocker Jun 03 '14 at 17:59
  • This answer might be interesting, as well: http://stackoverflow.com/a/5874292/2649012 - hard to tell, when I don't know which is **exactly** your goal (a picture might help). Does `a lot of space at the bottom` refer to the grid or to each of its items? – Phantômaxx Jun 03 '14 at 18:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55008/discussion-between-liarspocker-and-der-golem). – liarspocker Jun 03 '14 at 18:15
3

Solved it by putting the ImageView in a RelativeLayout, setting the layout_below item in the RelativeLayout and the alignParentBottom item of ImageView:

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/gs_grid_view">
<ImageView
    android:id="@+id/gs_bottom_logo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:adjustViewBounds="true"
    android:maxWidth="200dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"/>
</RelativeLayout>
liarspocker
  • 2,434
  • 3
  • 17
  • 26