Android: How to resize image to fill parent? - android

I am working on application which shows images, and I need to create a view which shows big images in a 1:1 aspect ratio.
First, I have thumbnails which are at a 72x72 resolution. I have put them in an ImageView but even if I add the parameters android:width="fill_parent" and android:height="wrap_content", the image would not be not the size of the ImageView but is only centered in the mid.
I tried adding the parameter android:scaleType="centerCrop" which resized the image well horizontally but the height remained the same.
How could I set up my ImageView so that it would be on all devices as width as the parent view and it should also be as height.
I can do this programatically, but I need to be able to do it in XML.
My XML File:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
// The ImageView I am talking about above
<ImageView
android:id="#+id/invitation_imageview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:scaleType="centerCrop"
android:src="#drawable/face_woman_smile" />
<ImageView
android:id="#+id/invitation_avatar_view_1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:src="#drawable/face_woman_smile"
android:scaleType="center"
android:layout_margin="1dp" />
</LinearLayout>

I think you need to add this to your ImageView
android:adjustViewBounds="true"

Use:
android:scaleType="fitXY"

To make image stretch to parent's width while keeping aspect ratio
Set
1- android:layout_width to match_parent
2- layout_height to wrap_content
3- adjustViewBounds to true
Like this:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/unknown"
android:adjustViewBounds="true" />
Note: This might not render correctly in Preview but would work on runtime.

I usually use android:width="match_parent" and android:height="wrap_content", as you mentioned, but I use android:scaleType for my ImageView layouts.
You can read more about it here.

Related

How to auto width and height?

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.

Android: Set size of ImageView (GIF) to dimensions of source

:)
I am using an ImageView and I display gifs in it. The problem is for example this gif: http://media3.giphy.com/media/110U0ZUA6mPxqE/200.gif has different dimensions than this: http://media0.giphy.com/media/10YsmVhdN1S6wE/200.gif and I display the gifs in a listview. The gif which has more or less the same height and width is shown good. But the gif which has unreasonably different width and heigt shows only a part of the gif..
This is my Item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageViewGif"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:minWidth="300dp"
android:visibility="visible" />
</LinearLayout>
<ProgressBar
android:id="#+id/progressBarSearchedGif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
To sum up, the question is how can I show the full gif in my imageview even when the dimensions of the gifs are different.
you can try removing android:scaleType="fitCenter" , and set android:adjustViewBounds="true" to your imageview.
If you always want the entire image to display in an ImageView, you have two choices. You can either stretch it to fit the size of the ImageView with this option:
android:scaleType="fitXY"
Or you can reduce the size of the image to fit the ImageView without changing its aspect ratio:
android:scaleType="centerInside"
Refer to the javadoc for details.

Image with same resolution not fit on layout imageview

Problem: trying to give multi device support screen for a simple layout but in Nexus 6 its creating the problem,means not showing fit.
It's side is cutting down where as per as docs for nexus-6 resolution is 1440*2560 and image should be in drawable-xxxhdpi .so i keep a image with resolution of 1440*2560.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:id="#+id/imv" />
</RelativeLayout>
if i ll give match_parent so obvious it will stretch the image.
Please help me to get out of it.
Here is screen shots of screen -
http://s10.postimg.org/43od5j5h5/screen_shot_layout.png
Set the width of the Image to fill_parent (you can set margins if you want a bit smaller imageView or you could also just set a fixed width)
Use android:adjustViewBounds to keep aspect ratio
Set height to wrap_content (it will take whatever it takes to maintain aspect ratio)
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/icon"
android:id="#+id/imv"
android:scaleType="fitCenter" />
If you want to fit the image on the imageview, replace android:src with android:background in the ImageView in the xml code.
Try to use these lines of code in the layout
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:scaleType="fitXY"
android:id="#+id/imv" />

How can I make my image bigger on Android with XML?

I have an image on my ImageView but I don't know how to make it bigger with XML. My image it's the following:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/img"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:src="#drawable/image"/>
I tried changing android:layout_width and android:layout_height attributes but if I put them with a number (for example, 200dp width and 100dp height) the ImageView it's on the left of the screen and not center.
I want to center the image and makes it a little bit bigger than the original. I couldn't find anything to makes it bigger at the same time it is displayed on the center of the screen.
Is it possible? How can I do that?
Thanks in advance!
Use scaletype and add android:adjustviewbounds="true" in your image view section.
Keep the size you want as the width and height of the ImageView and change the gravity of its parent to center
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/img"
android:layout_marginTop="40dp"
android:src="#drawable/image"/>
</LinearLayout>
EDIT:
If this is what you want your layout to look like
then all you would need to do is add
android:layout_gravity="center_horizontal"
to your ImageView, and specify the width and height in dp
There are 3 attributes you can add to your imageview:
android:scaleType=""
android:scaleX=""
android:scaleY=""
ScaleType types can be seen here. Basically, you'll want to use CENTER_CROP.
scaleX and scaleY uses float as parameters, defining the scale for the two axis. See the reference here.
Both codes above works (Andrew Brooke and IntelliJ Amiya, Thanks!) but now I found a "trick" to avoid both of these methods. It's to create the ImageView using android:minWidth and android:minHeight so the final ImageView will be like:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/img"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:minWidth="100dp"
android:minHeight="70dp"
android:src="#drawable/image"/>
With that, you can center your ImageView with the size that you want.

How to press ImageView to the borders

I have a problem. I have an ImageView in layout with different size. I want to set that ImageView to the bottom of the screen. That Picture must press to bottom / right / left borders without space. But in my case in some devices i have space between picture and border(left|right). Whats the reason?! Is it because of size of the picture or incorrect code? How to make it equal in all devices?!
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/man"
android:id="#+id/man"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="150dp"/>
Just add the following attribute to your ImageView:
android:scaleType="fitXY"
ScaleType specifies how you want your image to behave with different scales, and here we forced it to occupy the full Width and Height specified for it no matter it's size.
Put the imageview inside a relative layout with width of "match_parent" and height of "match_parent",
and add the following attribute to your imageView : android:layout_alignParentBottom="true"
Try this:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/man"
android:scaleType="fitXY"
android:id="#+id/man"
android:layout_alignParentBottom="true"/>

Categories

Resources