This is your gridview add to gridview.xml
<GridView android:id="@+id/clueGrid" android:layout_width="280dp" android:layout_height="match_parent" android:layout_below="@id/gameInstructions" android:layout_centerHorizontal="true" android:horizontalSpacing="20dp" android:verticalSpacing="20dp" android:paddingTop="10dp" android:numColumns="2" />
add this to another xml -> grid_view_image.xml
<com.XXXX.view.SquareRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/grid_item_background" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/clue_found" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:layout_marginBottom="10dp" android:layout_alignParentBottom="true" android:id="@+id/itemView"/> </com.XXXX.view.SquareRelativeLayout>
and that xml uses SquareRelativeLayout, add this Java
public class SquareRelativeLayout extends RelativeLayout { public SquareRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ super.onMeasure(widthMeasureSpec, widthMeasureSpec); } }
Then you need to create your adapter
Public class ItemGridAdapter extends BaseAdapter {
List<Item> itemList; public ItemGridAdapter(List<Item> itemList){ this.itemList = itemList; } @Override public int getCount() { return itemList.size(); } @Override public Object getItem(int position) { return itemList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null){ LayoutInflater inflater = LayoutInflater.from(parent.getContext()); convertView = inflater.inflate(R.layout.grid_view_image, parent, false); } final ImageView itemView = (ImageView) convertView.findViewById(R.id.itemView); itemView.setImageDrawable("YOUR DRAWABLE"); return convertView; } }
Then on your main class using gridview.xml
ItemGridAdapter itemGridAdapter = new ItemGridAdapter(itemList); itemGrid.setAdapter(itemGridAdapter);
Then you can only play with your ImageView and layout. HopeFully it helps!
I saw that you are using Picasso, here is my answer for the adapter that sets the image:
Target target = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { itemView.setImageBitmap(bitmap); } @Override public void onBitmapFailed(Drawable errorDrawable) {