I have a layout in which a linearlayout contains imageview as its direct child. I want to fit the width of this imageview so that it spans horizontally to match the size of the parent which will take the width of the whole screen.
How can I do that? This is what i've done so far:
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_margin="0dp"
android:src="#drawable/policemen_salute"
android:id="#+id/banner"
/>
</LinearLayout>
</RelativeLayout>
It looks like this
Thanks!
Add the 'scaletype' attribute:
<ImageView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:scaleType="fitXY"
android:layout_margin="0dp"
android:src="#drawable/policemen_salute"
android:id="#+id/banner"/>
See
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
You have to use a scaleType to scale correctly the image.
try your ImageView like this:
<ImageView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_margin="0dp"
android:src="#drawable/policemen_salute"
android:id="#+id/banner"
android:scaleType="centerCrop"/>
You can use android Scaletype Property .
android:scaleType="fitXY"
Then set android adjustviewbounds true from XML .Try this way I hope it will helps you.
If you wish to scale your image, you might want to add
android:scaleType="fitXY"
(Scale in X and Y independently, so that src matches dst exactly)
to your ImageView
<ImageView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_margin="0dp"
android:scaleType="fitXY"
android:src="#drawable/policemen_salute"
android:id="#+id/banner"
/>
Related
I want to display an image in Imageview with full height and also keep the aspect ratio of the image. To view the remaining part of the image, I want horizontal scrolling on that image. Please share the idea behind it?
As mentioned by ResolutioN, adjustViewBounds does the trick, but fitXY is not necessary. Just wrap your ImageView inside a HorizontalScrollView
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:src="#drawable/image" />
</HorizontalScrollView>
Combine adjustViewBound and scaleType="fitXY" to achieve your goal. This will keep the aspect ratio of the image. Like this:
<ImageView
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
Hope this code helps you:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/horizontalScrollView"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</HorizontalScrollView>
I use the following code to show my button
(Because I need to add the text on the button, so I will not use the ImageButton)
<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="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/bottom001"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</RelativeLayout>
But the ImageView will have some empty area in the View
I think "fitXY" will fill the View, but it didn't
How can I do in this case?
Thanks for your help :)
set android:layout_width="wrap_content" And Use LinearLayout
Finally
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/test"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
For details please read android-imageview-scaletype-samples
FIT_XY
Scale the image using FILL.Stretch the picture inside the available width and height, disgarding the aspect ratio.
I have an ImageView which is populated from database (the image comes from database). in the database i have images both portrait and landscape!! how can i adjust the imageView to scale it self according to the image width and height. I have tried many ways but no result! any help please!!!!
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slideshow"
android:orientation="horizontal"
android:padding="0dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginRight="5dp"
android:id="#+id/slide1"
android:src="#drawable/ddd"
/>
Try this code. adjustViewBounds attribute makes the ImageView the same size as image that you put in it.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
If you need specific width or height change the wrap_content value.
I have a background image in full screen with a RelativeLayout. And other image (100x100) in an ImageView.
I want to put the ImageView in center screen. But my image shows top left.
What is the problem?
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/fondo_inicio" >
<ImageView android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/icono_inicio"/>
</RelativeLayout>
try this to your ImageView
android:layout_centerInParent="true"
Add the following to the ImageView.
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/fondo_inicio" >
<ImageView android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:background="#drawable/icono_inicio"/>
</RelativeLayout>
<ImageView
android:id="#+id/pic_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:src="#drawable/pic" />
Try this
android:layout_gravity="center"
User layout_gravity and gravity doesn't work in case of RelativeLayout so just use
Layout_centerInParent = "true"
I have a large image myImage.png (its dims are 1536 x 784):
I want this image to be placed at the bottom of my layout scaled uniformly and fitting the width:
Layout is
<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="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/myImage"
android:adjustViewBounds="true"
android:layout_alignParentBottom="true"
android:scaleType="fitCenter" >
</ImageView>
</RelativeLayout>
I've tried dozens of combinations for layout_width, layout_height, adjustViewBounds and scaleType and nothing helped: it always looks as follows:
Help me please.
As someone says, use src instead of background:
<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="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/myImage"
android:adjustViewBounds="true"
android:layout_alignParentBottom="true"
android:scaleType="fitCenter" >
</ImageView>
</RelativeLayout>
You are using an ImageView. ImageViews use the src attribute to indicate which image to use and to apply the scaleType to.
background, is, as name suggests, merely a background attribute, made to be displayed on that whole background thing.
try this:
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/myImage"
android:adjustViewBounds="true"
android:layout_alignParentBottom="true"
android:scaleType="fitcenter" >
</ImageView>