Search code examples
androidlistviewpicasso

android listview with image item resize image height proportionaly


I have a list view with this image item:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/llItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@color/white"

android:layoutDirection="rtl"
android:orientation="horizontal">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="5dp"
    android:background="@color/white"
    app:cardElevation="4dp"
    app:cardUseCompatPadding="true">

    <ImageView
        android:id="@+id/pollimg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"


        />
</android.support.v7.widget.CardView>

I need to load the image view in getview method into the array adapter as below:

                 @Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
              .... 
                etc
              .....
        try {
            Picasso.with(context)
                    .load(allpolls.get(position).getMediaUrl()).fit()
                    .placeholder(R.mipmap.icon).into(holder.img);
        } catch (Exception e) {
            holder.img.setVisibility(View.GONE);
        }
         }

My problem is that the imageview height is fix for all rows, i need to set imageview height as the same image height as the online image. so it can take its exact height


Solution

  • From this tutorial

    fit() is measuring the dimensions of the target ImageView and internally uses resize() to reduce the image size to the dimensions of the ImageView. There are two things to know about fit(). First, calling fit() can delay the image request since Picasso will need to wait until the size of the ImageView can be measured. Second, you only can use fit() with an ImageView as the target.

    That meaning, that if you are using fit() the image will be resized. Remove it.

    For further information, if you want to keep the image's original aspect ratio you should consider putting both height and width to wrap_content