Insert image into grid

I am trying to put an image in a gridview. But I do not understand where my gridview gets the presentation height from.

The mesh cell is smaller than the image. I want to show an image full in a cell. But I do not want to hardcode the size of the ImageView.

My codes

gridView in xml

<GridView android:id="@+id/gridview_movie_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:numColumns="auto_fit" android:columnWidth="185dp" android:stretchMode="columnWidth"/> 

My ImageView, which I am inflating.

  <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop"/> 

And my getView method in adapter class

 @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( mContext.LAYOUT_INFLATER_SERVICE); ImageView imageView; if (convertView == null) { imageView = (ImageView) inflater.inflate(R.layout.grid_view_item, null); } else { imageView = (ImageView) convertView; } Picasso.with(mContext).setLoggingEnabled(false); Picasso.with(mContext) .load(imageResource[position]) .into(imageView); return imageView; } 

This is what I get

This is what I get

And this is what is required

enter image description here

+5
source share
4 answers

What worked for me was to add this line to my xml image view.

 android:adjustViewBounds="true" 

Now my conclusion is similar to what I wanted. Thanks for helping everyone.

+5
source

You tried in the getView override method to add parameters:

 imageView.setScaleType(ImageView.ScaleType.CENTER); // Replace default CENTER_CROP // Try ScaleType.CENTER or ScaleType.CENTER_INSIDE imageView.setPadding(8, 8, 8, 8); // why not be explicit about the padding while we're at it 

Good documents will also help:

0
source

Try fitXy on ImageView, it will try to fit along the edges without reducing the pixel

 <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY"/> 
0
source

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) { // Meh } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { // Meh } }; Picasso.with(convertView.getContext()) .load(imageUrl) .into(target); 
0
source

All Articles