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" />
Related
I am making app with Android Studio and want to auto set width and height of image that I am showing on front page. my code is like this
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/linearLayout1"
android:orientation="vertical"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView"
android:layout_width="375dp"
android:layout_height="220dp"
android:layout_marginBottom="5sp"
android:adjustViewBounds="true"
android:background="#drawable/japuji"
android:clickable="true"
android:focusable="true"
android:linksClickable="true"
android:scaleType="fitXY"
android:textStyle="bold" />
this is the scrollview and when i set fill_parent, wrap_content in TextView its not working either, when i test it on big screen images are small ,
i have tried all like changing width to fill_parent and wrap_content
You should not display an image as a background of a view, instead, you should use ImageView
<ImageView
android:contentDescription="#string/imagedesc"
android:id="#+id/my_image"
android:src="#drawable/your_image"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
src : sets the image drawable
scaleType : sets the desired scale type, in this case fitCenter with adjustViewBounds set to true and width or height (one of them) to wrap_content will show the full image.
EDIT :
if you have a fixed view size and you want to cover it all with that image drawable, you should use centerCrop as a scaleType instead of fitCenter
You're apparently displaying the image (android:background="#drawable/japuji") in the TextView, but the TextView has its height and width hard-coded: android:layout_width="375dp" android:layout_height="220dp"
Instead set the TextView's height and width to match_parent.
i'm trying to make ImageView take match_parent of RelativeLayout
like this picture :
when i run my app ImageView looks like that :
this my xml :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linealL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/khdar">
<RelativeLayout
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:id="#+id/RelativeLayout02"
android:layout_width="wrap_content"
android:background="#color/colorGriviewBorder"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_margin="5dp"
android:id="#+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/gradient"
android:orientation="vertical" >
<TextView
android:text="GIF"
android:textSize="16sp"
android:layout_gravity="center"
android:textColor="#color/colorGriviewBorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dev"/>
</LinearLayout>
<ImageView
android:padding="8dp"
android:layout_below="#+id/LinearLayout01"
android:contentDescription="#string/app_name"
android:id="#+id/imageSelectTo"
android:scaleType="fitXY"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
Change layout_width of ImageView to match_parent and add one new attribute to it: android:adjustViewBounds="true".
If you want to fit the ImageView width then You have to ignore height. And by default will not strech for you to keep aspect ratio, so you need to add another attributes for adjust view bounds. So lastly you will have your code chages as follows for an ImageView:
android:layout_width="match_parent"
android:adjustViewBounds="true"
The adjust View bounds will adjust to a particular height while keeping the aspect ratio.
More
Also to avoid future mistakes again in your code you have nested two RelativeLayout's which I guess is due backgrounds. If there is no need for that If do not care about space. The inner RelativeLayout View height and width should be set to match_parent!. Happy Coding!
Your ImageView's layout_width and layout_height are set to wrap_content instead of match_parent.
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.
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.
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"