Tanmay
Tanmay

Reputation: 49

Grid view in xml android

I want to display ImageButtons in GridView. Currently I am using RelativeLayout.

Here is my XML code:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <RelativeLayout
    android:id="@+id/RelativeLayout01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.easy.convert.MainActivity" >

    <ImageButton
        android:id="@+id/btnbitsbytes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:background="#00000000"
        android:gravity="center"
        android:src="@drawable/btnbitsbytes" />

    <ImageButton
        android:id="@+id/btnmassweight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnbitsbytes"
        android:layout_below="@+id/btnbitsbytes"
        android:layout_marginTop="14dp"
        android:background="#00000000"
        android:src="@drawable/btnmassweight" />

    <ImageButton
        android:id="@+id/ButtonConvert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnmassweight"
        android:layout_below="@+id/btnmassweight"
        android:layout_marginTop="14dp"
        android:background="#00000000"
        android:src="@drawable/btnlength" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ButtonConvert"
        android:layout_below="@+id/ButtonConvert"
        android:layout_marginTop="14dp"
        android:background="#00000000"
        android:src="@drawable/btntemperature" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton3"
        android:layout_below="@+id/imageButton3"
        android:layout_marginTop="15dp"
        android:background="#00000000"
        android:src="@drawable/btndistance" />

    <ImageButton
        android:id="@+id/imageButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:background="#00000000"
        android:src="@drawable/btnaboutus" />

    </RelativeLayout>
</ScrollView>

I dont know how to use GridView or any other Layout but i want my layout to look something like this:

enter image description here

Upvotes: 0

Views: 5137

Answers (1)

Blo
Blo

Reputation: 11978

You don't need a ScrollView with GridView. The gridview widget has its own scroll detection which it can disturbe the scrollview scroll. I'd suggest you a few thing to achieve what you want, try to put inside your scrollview a TableLayout, this will be more adapted:

<ScrollView ... >
    <RelativeLayout ... >
        <TableLayout ...>
            <TableRow>
                 <ImageButton>
                 <ImageButton>
                 <ImageButton>
            </TableRow>
            <TableRow>
                 <ImageButton>
                 <ImageButton>
                 <ImageButton>
            </TableRow>
            <TableRow>
                 <ImageButton>
                 <ImageButton>
                 <ImageButton>
            </TableRow>
    </RelativeLayout>
</ScrollView>

Or you can just add 3 LinearLayouts and set the weight attribute of each children to 1, your layout will be something like that:

<ScrollView ... >

    <RelativeLayout ... >

        <LinearLayout ...
            android:orientation="horizontal"
            android:weightSum="3" >

            <ImageButton   ...
                android:layout_weight="1" />

            <ImageButton  ...
                android:layout_weight="1" />

            <ImageButton  ...
                android:layout_weight="1" />

        </LinearLayout>

    <RelativeLayout>

</ScrollView>  

weightSum set to 3 indicates the number of columns in the linearlayout and layout_weight indicates that each child will fill 1 column.

But if you absolutely want a GridView, you'll need this layout:

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth" />

You'll also need an Adapter to display your image inside. You should read this topic: GridView. But a GridView doesn't allow to have a footer view like your Button "About us"..
To keep the Button "About us" at the end, you need a custom adapter for your gridview, maybe this will help you: HFGridView - It works like addFooterView for ListView.

Upvotes: 1

Related Questions