Victor Yew
Victor Yew

Reputation: 35

Android list view image does not fit screen width

I am having this list view where the image view in it does not fit the screen width even if the size of the bitmap is larger than the screen size. Here is the result i get:

(Due the spam policy, I cannot post my screenshots over here. Please click on the links below to view the screenshots.) http://www.sundancepost.com/ivue/screen/screen1.jpg

As indicated by red boxes in the screen above, the list does not fit the width of the screen.

The following is the result I wanted:

http://www.sundancepost.com/ivue/screen/screen2.jpg

Here is my code for the layout.

featured_listrow.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:orientation="vertical">    

<ImageView
   android:id="@+id/banner"        
   android:layout_width="fill_parent"       
   android:layout_height="wrap_content"
   android:adjustViewBounds="true"
   android:src="@drawable/ampersand_banner"/>

</RelativeLayout>

featured_list:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:smoothScrollbar="true" 
    >

</ListView>

</LinearLayout>

I believe I've set the layout_width and the layout_height correctly for both the layout files. I think this have something to do with the android:adjustViewBounds setting during runtime. Can somebody please help me?

Upvotes: 1

Views: 2275

Answers (3)

urnish
urnish

Reputation: 469

You can set your Image in your image view either using

1) android:scaleType="fitXY"

or

2) android:scaleType="centerCrop"

Use any one in your image view. it gives me perfect result as i want.

Upvotes: 1

Victor Yew
Victor Yew

Reputation: 35

I finally found out what is the problem. The scaling of the images happens in this ImageLoader.class found in http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

So, when I commented out the scaling part of the code, it all back to normal.

This is the code:

private Bitmap decodeFile(File f){
    try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
       // final int REQUIRED_SIZE=100;
       // int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
      //while(true){
        //if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
        //break;
        //width_tmp/=2;
        //height_tmp/=2;
        //scale*=2;
       //}

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}

Upvotes: 2

Kęstas Venslauskas
Kęstas Venslauskas

Reputation: 401

I think the only thing that could help here is resizing images using other programs. The problem is that image cannot be cropped programaticaly just horizontal or vertical sides it always do both when setting image size on xml or in code.

Upvotes: 0

Related Questions