For example I already have 7 buttons in a GridView
, i just want that in place of 8th and 9th button i get one image.
Is it possible to do anything like that?
1 Answers
Yes it's possible,
as pointed in this answer:
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.
but the only possible thing to achieve what you looking for is by creating a custom GridView that has a customized view element as pointed in this answer:
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 your elements, like Button and ImageView in your case: grid_element.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" >
<Button
/>
<ImageView
/>
</FrameLayout>
</RelativeLayout>
finally, after declaring the GridView and assigning grid_element.xml
as the GridAdapter view type, just iterate over every element and set the Visibility of it's children as Visible
, Gone
respectively or Gone
, Visible
respectively according to if you want to make the Button visible or the ImageView. And to set the onClick
for the Buttons:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if(position != 8 && position != 9){
// do onClick work
}
}
});

- 1
- 1

- 8,914
- 14
- 83
- 118
-
Thank you so much. The same thing i was looking for – Asad Khan Apr 12 '16 at 07:39