Custom ListView where all images are the same size
I want to create ListView
where each line has a rectangular image and some text. For example:
The problem that I have is that some image is height higher than width and then ListView
looks like this:
This is my actual code:
xml layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:id="@+id/resultItem">
<ImageView
android:id="@+id/spicture"
android:layout_width="97dp"
android:layout_height="60dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:adjustViewBounds="true"
android:layout_marginTop="3dp" >
</ImageView>
<TextView
android:id="@+id/sname"
style="@android:style/TextAppearance.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
And the custom adapter looks like this:
public class ListViewAdapter extends BaseAdapter {
private final Context mContext;
private ArrayList<Integer> imageId;
private ArrayList<String> textStr;
private final LayoutInflater inflater;
private static class ViewHolder {
ImageView imgView;
TextView txtView;
}
public ListViewAdapter(Context c) {
mContext = c;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return imageId.size();
}
@Override
public Object getItem(int position) {
return textStr.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
/**
* Rep per parametre un array amb els identificadors de les imatges i els
* string dels textes a mostrar
*
* @param idImages
* @param strText
*/
public void setData(ArrayList<String> strText, ArrayList<Integer> idImages) {
imageId = idImages;
textStr = strText;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.result_item, null);
holder.imgView = (ImageView) convertView.findViewById(R.id.spicture);
holder.txtView = (TextView) convertView.findViewById(R.id.sname);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromResource(mContext.getResources(), imageId.get(position), 50, 50);
holder.imgView.setImageBitmap(bitmap);
holder.txtView.setText(textStr.get(position));
holder.txtView.setGravity(Gravity.CENTER);
return convertView;
}
}
How can I get all rectangles the same size?
source to share
Either you crop the image before installing ListViewItem . Or you just use images with the same ratio.
But I guess you are looking for an easier solution. Just addscaleType="centerCrop"
<ImageView
android:id="@+id/spicture"
android:scaleType="centerCrop"
android:layout_width="97dp"
android:layout_height="60dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:adjustViewBounds="true"
android:layout_marginTop="3dp" >
You can find more information about scaleTypes
here: http://www.techrepublic.com/article/clear-up-ambiguity-about-android-image-view-scale-types-with-this-guide/
The fitXY note will scale it to fit, but the original ratio will be lost.
source to share
Use this one android:scaleType="fitXY"
for ImageView
, try below code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:id="@+id/resultItem">
<ImageView
android:id="@+id/spicture"
android:layout_width="97dp"
android:layout_height="60dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:adjustViewBounds="true"
android:layout_marginTop="3dp" >
</ImageView>
<TextView
android:id="@+id/sname"
style="@android:style/TextAppearance.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
source to share