Wrap Content ImageView display full size of image

2.8k Views Asked by At

I am using imageview in android and when I put image in imageview using attributes wrap_content for both height and width. Images appear to be so much big as they are of real size or if i took small images they got blur.

code for it


         <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/showhide"
        android:layout_marginRight="20sp"
        android:layout_marginLeft="20sp"
        android:layout_marginTop="10sp"
        android:padding="5sp"
        android:weightSum="4"
        >

        <!-- delivery intimation-->
        <LinearLayout android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/layout_d_i"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:weightSum="2"


            >
            <ImageView android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:src="@drawable/delivintimation"
                android:id="@+id/delivery_intimation"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:layout_marginBottom="10sp"

                />

            <TextView android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Delivery Intimation"
                android:gravity="center"
                android:maxLines="100"
                android:singleLine="false"
                android:layout_below="@id/delivery_intimation"
                android:textSize="12sp"
                android:textColor="@android:color/black"
                />

        </LinearLayout>
          </LinearLayout>

7

There are 7 best solutions below

0
On

Use fixed size width and height on imageview.

<ImageView
    android:layout_width="200dp"
    android:layout_height="180dp"
    android:id="@+id/imageView"
    android:src="@drawable/image"
    android:scaleType="fitXY"/>
1
On

Use LinearyLayout to wrap your ImageView and another invisible View, and set layout_weight both to 0.3 or 0.5 etc, then the ImageView will be according to your size.

Here is an example for you showing how to set the image half the screen size

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:background="#ff0000"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/your_image"
            android:adjustViewBounds="true"
            />
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:background="#00ff00"
        />
</LinearLayout>

If you want to put your image in center of the screen, you can use the same idea and set proper weight to achieve your goal

0
On

Did you try using android:scaleType attribute?

Example:

android:scaleType="centerCrop"

This attribute uses many other values like, 'fitXY', 'center', 'centerInside' etc. Try this

0
On

You can manually give width and height programatically

<ImageView   android:layout_width="100dp"
android:layout_height="200dp"
android:src="@drawable/notes"
android:id="@+id/iv_notes"
android:scaleType="fitXY"/>

scaleType-- fitXY will compress and fit all image inside imageview scaleType-- centerCrop will crop the image to the image view size

If you dont want to hard code the Width and height you have to create different drawable folders and place your different resolution images in different folder , Examples of drawable folders are

 drawable-ldpi
 drawable-mdpi
 drawable-hdpi
 drawable-xhdpi
 drawable-xxhdpi
 drawable-xxxhdpi
0
On

Use android:scaleType="centerInside"

0
On

use src instead of background in xml please give the xml to see how you implemented it

2
On

The code which you have used gave you the correct result: you have used LinearLayout and used weights. weights will divide according to the screen resolution. just remove LinearLayout and use Relativelayout instead. Next remove all the weights for images and set its attributes to wrap_content. If you want to give images that fits exactly to your screen width do it programmatically.

 DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
int desiredWidth=width/2;
yourView.setHeight(desiredWidth);

if this is not your requirement then in your RelativeLayout where you have these images. give attributes . android:alignParentRight="true" for one and android:alignParentLeft="true" for another image and set margins.