Search code examples
androidandroid-layoutgridviewandroid-relativelayout

Fill up the remaining space for GridView after a ImageView in Relative Layout


I am making a view through Gridview and ImageView in XML where the ImageView shows the selected image and the gridview shows all the image.

I have a Imageview lets say of 300 dp of width and 200 dp of height which I am setting up using the below code.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"     
android:background="@drawable/bg">
<RelativeLayout 
android:id = "@+id/relative1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">    
<ImageView
   android:id="@+id/imageView1"
   android:layout_width="300dp"
   android:layout_height="200dp"
   android:adjustViewBounds="true"
   android:layout_alignParentLeft="true"
   android:src="@drawable/ic_launcher" />
</RelativeLayout>
 <GridView 
android:id="@+id/imageGallery"
android:paddingTop="10dp"
android:layout_below="@+id/relative1"
android:stretchMode="columnWidth"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clipChildren="true"
android:horizontalSpacing="5dip"
android:verticalSpacing="5dip"
android:numColumns="2"
android:fadeScrollbars ="false"
>  

Now I want that my gridview takes up all remaining height after the ImageView sets up. Cause I do not want extra blankspace on the bottom. I know I can set up the GridView to align bottom to parent however that looks odd.


Solution

  • Here is how to do that by using a RelativeLayout

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg"
        >
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:adjustViewBounds="true"
            android:layout_alignParentLeft="true"
            android:src="@drawable/ic_launcher"
        />
        <GridView
            android:id="@+id/imageGallery"
            android:paddingTop="10dp"
            android:layout_below="@+id/imageView1"
            android:stretchMode="columnWidth"
            android:cacheColorHint="#00000000"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipChildren="true"
            android:horizontalSpacing="5dip"
            android:verticalSpacing="5dip"
            android:numColumns="2"
            android:fadeScrollbars ="false"
        >
    </RelativeLayout>
    

    Note that I removed the useless RelativeLayout that was surrounding the ImageView.
    Here the GridView is taking ALL THE REMAINING SPACE to fill the parent.