Gridview with layout (imageview + imagebutton) for every value

15,094

Solution 1

Short answer: Yes. You can have an ImageView and an ImageButton in a GridView.

Long answer:

You will naturally have to create a custom GridView for that purpose.

For example:

Create an XML which will hold the container GridView, say, grid.xml:

<GridView
    android:id="@+id/gridFriends"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clipChildren="true"
    android:columnWidth="100dp"
    android:fastScrollEnabled="true"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth" >
</GridView>

And, to define the contents of the GridView, create another XML layout which will hold the ImageView and the ImageButton. Say, grid_items.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp" >

    <FrameLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="center" >

        <ImageView
            android:id="@+id/imgProfilePicture"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@null" />

        <ImageButton
            android:id="@+id/imgbtnDemo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:background="@null"
            android:gravity="center"
            android:src="@drawable/ic_contact_picture" >
        </ImageButton>
    </FrameLayout>

</RelativeLayout>

Finally, if you are familiar with the concept of custom ListViews, with a few modifications, you will be able to implement a custom GridView too. If you are not familiar with custom ListViews or GridViews, follow this tutorial to see how to create a custom GridView: http://www.coderzheaven.com/2012/02/29/custom-gridview-in-android-a-simple-example/. Or use this Google Search for more tutorials on the same.

An important point here would be, if you need the ImageButton's to do a function when they are clicked, the onClickListener will need to be setup in the Adapter.

Solution 2

GridView shows a grid of Views. It can show anything that extends View class. It can show a LinearLayout with an ImageView and an ImageButton inside it.

Share:
15,094
rosu alin
Author by

rosu alin

I work at a programming firm from The Netherlands, Amsterdam named 100Grams Software B.V.. I am an Android Developer, making all kind of smartphone applications for clients and also working on a startup called Passenger. I also work a bit in C++/CX but I am new to it, so I might need help.

Updated on June 04, 2022

Comments

  • rosu alin
    rosu alin almost 2 years

    Is it possible to make a gridview that has instead of a grid of pictures, a grid of pictures with a small imagebutton below every one of them?

  • rosu alin
    rosu alin over 11 years
    thanks, this site: coderzheaven.com/2012/02/29/… + the xml's you gave me helped a lot in doing my custom gridview. Very good answer
  • Pria
    Pria over 10 years
    How can I inflate the relative layout in the grid_items? I am getting this exception: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
  • Siddharth Lele
    Siddharth Lele over 10 years
    @Pria: You seem to be casting it wrong. If it is a RelativeLayout, you should be casting it as that. You seem to be casting a FrameLayout. Alternatively, post a new question and link to it here. I'll see if I can help you out.