LinearLayout ImageView Resize All Image Widths To Fit - android

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"

Related

Wrap Content ImageView display full size of image

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.

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

LinearLayout too wide / wrong size calculation of images

I have a LinearLayout that has only images in it. The image size is reduced as the height of the layout is not sufficiant. Images are sized correctly.
However: The width of the LinearLayout is too wide. It is actually the size the images would have if adjustviewbound=false would be set instead of true as it is in my case.
This appears to be a problem with the RelativeLayout around it. If I use a LinearLayout for the page, the calculation is correct.
Any ideas how to get the correct width? (no, I can not change the RelativeLayout).
Here is the layout for you to try (I used Android 4.3 API 18):
<?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" >
<LinearLayout
android:id="#+id/map_Test"
android:layout_width="wrap_content"
android:layout_height="10dp"
android:background="#color/red"
android:orientation="horizontal" >
<ImageView
android:id="#+id/map_DrillDownImage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#android:drawable/ic_dialog_alert" />
<ImageView
android:id="#+id/map_DrillDownImage2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:adjustViewBounds="true"
android:src="#android:drawable/ic_dialog_email" />
</LinearLayout>
</RelativeLayout>

Does LinearLayout have a max height?

I have a vertical linearlayout that won't stretch beyond a certain limit.
This is the layout with centerCrop in the imageview
http://tinypic.com/r/20nm6s/5
This is the layout with no crop set (so it should be full width and huge)
http://tinypic.com/r/vzk7kw/5
So my thought is that there is some implicit max height that I'm not seeing in my layout but I can't see where it is, can you spot my error?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/dropshadow"
>
<TextView
android:id="#+id/heading"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<ImageView
android:id="#+id/featuredimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="1000dp"
android:scaleType="centerCrop"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</RelativeLayout>
The "implicit" max height of the layout is the height of its parent. Since you're using wrap_content on both layouts, that means the parent is effectively the screen area, minus whatever other views you're using (such as the TextView). Unless you place it in a scrolling container, such as a ScrollView, it won't ever exceed the size of the screen.
The reason your ImageView isn't showing up "full width and huge" when you remove the crop is because the default scaleType for an ImageView is fitCenter. This particular view is bounded by the layout's width, so it shrinks the image while maintaining aspect ratio.

android, imageviews inside linear layout don't fit the screen size

I was wondering how to make the four images inside my LinearLayout look as big as possible depending on the screen. When I install the widget in my phone, it always fits only 50% of the screen.
Here comes the xml.
Any hints?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|clip_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="#+id/hora1_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/neutro_on" />
<ImageView
android:id="#+id/hora2_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/neutro_on" />
<ImageView
android:id="#+id/minuto1_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/neutro_on" />
<ImageView
android:id="#+id/minuto2_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/neutro_on" />
Add android:weightSum="1" to your parent linear layout.
The linear layout should have android:orientation="horizontal"
Now to each of your image views , add android:layout_weight = 0.25.
Hope this works.
hope you read enough about orientation vertical and horizontal .
you should clear here that by filling screen whether you mean altogether all 4 fills the screen such that each takes 25% or every one should take 100% width and added up vertically .
for first case add android:layout_weight=1 for every imageView .
for second case replace android:orientation="horizontal" by android:orientation="vertical" in root LinerLayout
Don't use wrap content, instead use the actual dimensions of the image or fill parent.

Categories

Resources