How to set an xml image resizing to its parent's height - android

I am doing an application that needs to use images, but the ImageView on the HorizontalScrollView isn't resized to its parent's height. How to do? I tried all, like centerCrop, ScaleType, etc... but nothing worked
<HorizontalScrollView
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="#dimen/scrollShareImgHeight">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sharedLinearLayoutImgs"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/loremipsum"/>
</LinearLayout>
</HorizontalScrollView>

Keep Linearlayout's height as "match_parent".

Related

Display an image with height 'matchparent' and also keep the aspectratio

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>

Remove white space from the bottom of screen Android

How i can remove this white space at the bottom of the screen. I have also tried android:adjustviewbounds and android:scaletype. But still failed to remove the white space at the bottom of the screen. Please help me to optimize my layout so that i can remove that ugly white space from the bottom.
Here is the screenshot below of my layout screen.
Here is my XML code of the layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
tools:context="com.example.zohai.v360.Fragments.Home">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_marginTop="-15dp">
<ImageView
android:id="#+id/intro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:background="#drawable/intro"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_marginTop="-15dp"
>
<ImageView
android:id="#+id/photog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/photographers"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_marginTop="-14dp">
<ImageView
android:id="#+id/model"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/model_bg"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
In your xml code you have mentioned
android:layout_height="190dp"
Which usually does not help in android:scaletype
Please your constrain layout or linear layout with weight to distribute 3 linear layout to the screen height. Like this,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="-15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="-15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="-15dp">
and then use android:scaletype to fit the LinearLayout
Note : Different screen size matters, I think in your case the screen height is high, which cause the layout to have blank space in bottom. In small size screen it may not be like that. Try doing above will help in all screen size
Your layout's height is set to wrap_content. The whitespace is there because you don't have enough size of the content to fill the screen completely. If you set the height to match_parent you could make the whitespace go away by setting android:background on the root view.
Otherwise you have to find out what you want there. Changing the size of the images is a bad idea because they will look stretched.
And because android is a big platform, you will see that on some devices the whitespace won't be there and on others it will be bigger. Don't set the size of the images to a fixed size.

android imageView scale

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.

Clickable image that matches screen width and image height

I am trying to create some clickable image in android that fills up the width of the screen, but the height of the clickable surface matches the height of the the image itself.
I have the following code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image1"
android:onClick="onClick2" />
</RelativeLayout>
None of the answers posted so far have helped me, FitXY doesn't help as I want to retain aspect ratio. The other answers regarding image button or changing the scale type, give me exactly the same problem.
the tag needed is android:adjustViewBounds="true"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height = "fill_parent" android:layout_width = "fill_parent">
<ImageView
android:layout_width = "wrap_content"
android:adjustViewBounds="true"
android:layout_height = "wrap_content" android:id = "#+id/ImageView1"
android:src = "#drawable/ic_launcher"/>
<ImageView android:layout_width = "wrap_content"
android:layout_height = "wrap_content" android:id = "#+id/ImageView2"
android:src = "#drawable/ic_launcher"
android:adjustViewBounds="true"
android:layout_below = "#+id/ImageView1"/>
</RelativeLayout>
Make use on linearLayout with vertical orientation
If i understand your problem correctly, you can put the image height to fill_parent (match_parent for API 8 and above) like the following. Then the image has the same height as its parent element.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/image1"
android:onClick="onClick2" />
</RelativeLayout>
You have forget to set the width of Your image view to match_parent.Also, set the relativeLayout to android:layout_height="match_parent". But it is better to put an ImageButton:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="#+id/imageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/image1"
android:scaleType="centerInside"
android:onClick="onClick2"
android:layout_alignParentTop="true" />
</RelativeLayout>
And You can play a little bit with the scaleTypes (it´s also available at imageView). In the relativeLayout, You have to set for example layout_alignParentTop="true" to set a view to the top.
It is best to make the parent layout be a LinearLayout and set the height with a defined value, and do it like this.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="#drawable/image1"/>
</LinearRelativeLayout>
OR
add weight to the imageview itself, but still has a LinearLayout as a parent.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" //This could be like 0.1, 0.7, so long as not 0
android:src="#drawable/image1"/>
</LinearRelativeLayout>
Adjusting the weight itself gives you control to the image in a manner of percentage.
Set ScaleType of ImageView to centerCrop or fitXY.
<ImageView
android:scaleType="centerCrop" />
Try this way,hope this will help you to solve your problem.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher"
android:onClick="onClick2" />
</RelativeLayout>

Android: Vertical LinearLayout - Two image views, I want the bottom one to scale to be the width of the top one

I'm trying to get a vertical LinearLayout to display an imageview with a second imageview below it. I want the lower imageview to be the width of the top image view.
I'm trying the current layout xml:
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:layout_weight="1"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/spacer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/spacer"
/>
</LinearLayout>
</LinearLayout>
The images will be displayed with a 'spacer' which is the full width of the screen, rather than the width of the image. The spacer is a fading edge graphic and I want it to be the same width as the image above it.
Note: the image for the top imageview is set programatically and could be a landscape or portrait image.
Anyone any thoughts on how to achieve this?
Thanks in advance!
A RelativeLayout will do. Use layout_below to achieve the vertical ordering and layout_widht="match_parent" to make the bottom image view as wide as possible with layout_alignLeft and layout_alignRight constraints:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerInside"/>
<ImageView
android:id="#+id/spacer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/image_view"
android:layout_alignLeft="#id/image_view"
android:layout_alignRight="#id/image_view"
android:background="#drawable/spacer" />
</RelativeLayout>
Sample output:
Using a slighly altered layout for demo purposes, warnings caused by missing contentDescriptions:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<ImageView
android:id="#+id/image_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:background="#f00"/>
<ImageView
android:id="#+id/spacer"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_below="#id/image_view"
android:layout_alignLeft="#id/image_view"
android:layout_alignRight="#id/image_view"
android:background="#0f0" />
</RelativeLayout>
(Yes I know. Old question but no good answers and got bumped up by Community.)
Hey r3mo use the following code you can get what you want to achieve
<RelativeLayout android:layout_height = "fill_parent" android:layout_width = "fill_parent">
<ImageView android:layout_width = "wrap_content"
android:layout_height = "wrap_content" android:id = "#+id/ImageView1"
android:src = "#drawable/spacer"/>
<ImageView android:layout_width = "wrap_content"
android:layout_height = "wrap_content" android:id = "#+id/ImageView2"
android:src = "#drawable/spacer"
android:layout_below = "#+id/ImageView1"/>
</RelativeLayout>
Use Table Layout Instead of Relative and LinearLayout To achieve this, as Table Layout Automatically provide same column with to all rows.

Categories

Resources