Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

use LinearLayout with weight

<LinearLayout 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:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:layout_alignParentLeft="true"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher" />

    <GridView
        android:id="@+id/imageGallery"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/relative1"
        android:layout_weight="1"
        android:cacheColorHint="#00000000"
        android:clipChildren="true"
        android:fadeScrollbars="false"
        android:horizontalSpacing="5dip"
        android:numColumns="2"
        android:paddingTop="10dp"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dip" />

</LinearLayout>

to cover all your screen use "android:layout_weight". like this

<GridView
    android:id="@+id/imageGallery"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top