Wrap Content ImageView display full size of image - android

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>

Use
android:scaleType="centerInside"

Did you try using android:scaleType attribute?
Example:
android:scaleType="centerCrop"
This attribute uses many other values like, 'fitXY', 'center', 'centerInside' etc. Try this

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

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

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

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"/>

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.

Related

How to set imageview height to match image ratio in LinearLayout?

I have a list of Imageviews that have their width set by the weight of a linear layout. How can I set the height of the Imageviews to ensure that they keep the original aspect ratio of the bitmaps being loaded into the image.
Here is a portion of my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ll_auction"
android:weightSum="100"
android:paddingBottom="10dp">
<ImageView
android:layout_width="0dp"
android:layout_weight="30"
android:layout_height="#dimen/auction_image_height"
android:layout_gravity="center"
android:background="#drawable/image_view_background_unselected"
android:layout_margin="2dp"
android:adjustViewBounds="true"
android:id="#+id/niv_auctionImage1" />
You can add an attribute called scaletype to the imagevie and set it to fitxy
Sample
android:scaleType="fitXY"
It will auto scale your image base on the wieght you specify in your attribute
If you are specifying a width through the layout weight, AND you want the picture the keep its aspect ratio, you need the height to then be wrap_content. Adjust view bounds is also correct, and I would use the scaleType fitCenter.
You also want to make sure you set the image to be scaled as the src, not as the background, which will always be stretched.
Give this a go.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ll_auction"
android:weightSum="100"
android:paddingBottom="10dp">
<ImageView
android:layout_width="0dp"
android:layout_weight="30"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/image_view_background_unselected"
android:layout_margin="2dp"
android:src="#drawable/example_image"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:id="#+id/niv_auctionImage1" />

Putting ImageView on the bottom of the screen

I am trying to put the ImageView on the bottom of the layout with following XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EEEEEE"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:src="#drawable/photo" />
</LinearLayout>
</LinearLayout>
I am following the instructions found here http://sandipchitale.blogspot.co.uk/2010/05/linearlayout-gravity-and-layoutgravity.html
Only difference is that I use ImageView instead of Button as here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="bottom" >
</Button>
</LinearLayout>
</LinearLayout>
Which puts the button on the expected place:
Unfortunately this isn't the case in my XML on the top of the post. The ImageView isn't placed on the bottom as the Button in reference XML is.
Any ideas why that happens ?
You should change a setting in ImageView itself, to be precise set android:scaleType="fitEnd" on your ImageView.
The other answers seem to be workarounds, including your own answer. This is not a problem with a layout, but with where inside the ImageView the image is loaded when the dimensions of the image and the screen aren't similar, (i.e. the width/height ratio is very different). By default it would load the image in the middle of the ImageView.
Try using RelativeLayout instead of LinearLayout and use parameter android:layout_alignParentBottom = "true".
EDIT: Try giving layout_height and layout_width as wrap_content for your LinearLayout that holds your ImageView.
Or it might be that the image you are using has extra space above and below it.
UPDATE:
The problem is with the size of the image. It is much larger than the screen size. I would suggest you to programmatically read the screen size of the device with below code:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
and set the height and width for imageView programmatically as below:
image_view.getLayoutParams().height = height - 50;
image_view.getLayoutParams().width = width - 50;
Try this inside relative layout
<ImageView
android:id="#+id/imageView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:adjustViewBounds="?attr/isLightTheme"
app:srcCompat="#drawable/client_app_footer" />
tested & working as expected
you did not include android:layout_gravity="bottom" line in your imageview.
I have resized photo size from width 2560 to width 270 and finally ImageView went to bottom.

Center ImageView inside another ImageView in Android

Well I have to fit one ImageView inside another one. It is smaller and has to be exactly at the center. I have both images scaled for different screen resolutions but I can test only on one phone and I am wondering if I set the second image's height and width with dpi to fit my screen resolution will it be perfectly matched on all screens or is there any property in Android aside from setting the height and width that will automatically center one ImageView inside of other ImageView?
Edit: It's in RelativeLayout and the ImageViews are at the top left so I haven't added anything specific to the because they by default are there.
Edit2: Well changing the first ImageView to be RelativeLayout helps center the second image inside of it but then the RelativeLayout grows too big and enlarges the image that is background. Is there a way to make the RelativeLayout be as big as its background?
try this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="250dp"
android:layout_height="250dp" >
<ImageView
android:id="#+id/base"
android:layout_height="250dp"
android:layout_width="250dp"
android:src="#drawable/home"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<ImageView
android:id="#+id/center"
android:layout_height="150dp"
android:layout_width="150dp"
android:src="#drawable/home"
android:layout_centerInParent="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</RelativeLayout>
You cannot put an imageView inside another. Hope the following code will be helpful for you.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_image"
android:gravity="center" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image_on_center" />
</LinearLayout>
Use RelativeLayout and then use android:layout_centerInParent="true" remember the order you add the ImageViews will decide what is on top :)
Try this:
android:layout_gravity="center"

LinearLayout ImageView Resize All Image Widths To Fit

I have a horizontal linear layout that contains 3 image views. Each image view contains the same image which is 200x200 pixels. Here is my layout xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image200" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image200" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image200" />
</LinearLayout>
</RelativeLayout>
And here is what it looks like:
Notice how the third image from the left is resized I assume because there is not enough real estate to contain 3x 200 pixel wide images.
What I would rather happen is instead of shrinking only the last image, all three images are resized evenly so that they all fit across the screen. How do I setup my layout to evenly fit all three images?
Thank you
UPDATE - after changing my settings here is what things look like:
Notice that the border around the image view is still 200 px high. Why is this happening?
Set Width and height to match_parent for imageviews and set layout_weight=1 for all imageviews. Also set Attribute android:scaleType="fitXY"
This worked perfectly for me:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:adjustViewBounds="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageView
android:adjustViewBounds="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageView
android:adjustViewBounds="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
Layout width when set to 0dp, layout width is what came into picture. And here layout width of the LinearLayout is match parent. So it will take the whole width, and as per the layout width of the images, they take space in the ratio of there weight.
make your three images a width of 0dp
and add them a layout_weight of 1
;)
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/image200" />
#JanTacci Regarding your follow up question "Note, however, that even though the image has been shrunk the actual layout height is still 200 px. Why is this happening?"
I just had the same issue and fixed it by adding this property to each ImageView:
android:adjustViewBounds="true"

Does android:scaleType="fitCenter" only work with fix Layout_width and Layout_height attributes?

I use Layout_width="fill_parent" and Layout_height="wrap content". If an image is bigger than the ImageView, it will be downscaled perfectly.
However, I never got it working to upscale smaller images. I tried any combination of ScaleType and "AdjustViewBounds": it always stays in its own size in the middle of the image view. Here is the code...
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_height="wrap_content"
android:background="#drawable/content_bg"
android:orientation="vertical"
android:padding="5dp"
android:layout_width="fill_parent"
android:layout_marginLeft="1px">
<ImageView
android:layout_height="wrap_content"
android:src="#drawable/test"
android:id="#+id/ImageViewTest"
android:layout_width="fill_parent"
android:adjustViewBounds="true"
android:scaleType="center"></ImageView>
</LinearLayout>
I just found a way to make it work.
This works in 2.1(AVD) & 4.1.2(device)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
uses the original size of the image inside.
Try something bigger or fill_parent
Use android:scaleType="FIT_START" or android:scaleType="FIT_END", to Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst (from GoogleDev).
More info:
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Adding android:scaleType="fitXY" to ur ImageView should stretch the image if the size is small.

Categories

Resources