Clickable image that matches screen width and image height - android

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>

Related

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

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

Imageview not matching the width of Linearlayout

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

Prevent Image from scaling even though the imageView height = "match_parent"

I have this linear layout,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:background="#color/white"
android:layout_marginTop="1dp"
android:weightSum="1" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:gravity="left|center"
android:padding="5dp"
android:text="Policy No."
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Now i want the image to not scale but the ImageView to match the height of the parent, the TextView height is set to wrap content and that cant be changed. Also, i can't set the imageView height as wrap_content, since it'll change the height of the overall Linear Layout and i can't afford that.
![Image of problem]http://imgur.com/gaef828
Now what i want is to have the image's size remain the same as it is the second row, even though the first row's height has increased.
Thanks in advance, any help would be appreciated.
add :
android:scaleType="centerInside"
to your ImageView
To perform no scaling, use
android:scaleType="center"
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
To get what you want, you can do this:
First give your imageviews same fixed height so they won't scale:
android:layout_height="25dp"
Than put gravity to center_vertical:
android:layout_gravity="center_vertical"
So now you have for each imageview:
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_weight="0.2"
android:src="#drawable/ic_launcher"
android:layout_gravity="center_vertical" />
It now no longer matters how may lines your textview has, your imageview will stay the same!
Ouput:
Hope this will help you with your problem.

Android not resizing?

I am developing my app on a 4in emulator in Eclipse. My phone is a 4.6in android device and when I test it on there, the content doesn't scale correctly.
It looks like the phone keeps the content at the same size, and it just moves everything up and leaves the extra space empty.
From what I have read, I thought it was supposed to keep the same layout as long as I use dp and not in. Does that mean my images need to be dp as well?
Any suggestions?
EDIT:
Here is some code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/tfltestbg2"
android:orientation="vertical" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="55dp"
android:background="#drawable/tfl_header_port"
android:gravity = "center_horizontal"
android:layout_gravity = "center_horizontal"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_gravity = "center"
android:gravity = "center"
>
<Button
android:id="#+id/buttonTakePic"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:gravity = "left"
android:background="#drawable/takepic_button" />
<Button
android:id="#+id/mMegaGalleryButton"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity = "center"
android:background="#drawable/add_fotos" />
<Button
android:id="#+id/mGalleryButton"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:gravity = "right"
android:background="#drawable/selectpic"
android:paddingLeft = "10dp"
android:paddingRight = "10dp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/image_container"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center"
android:background="#drawable/outline_box"
android:gravity="center">
<FrameLayout
android:layout_width = "match_parent"
android:layout_height = "fill_parent"
android:gravity = "center"
android:layout_gravity = "center">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background = "#drawable/blank"
android:gravity="center" />
<ImageView
android:id="#+id/picsImage"
android:layout_width = "100dp"
android:layout_height = "100dp"
android:layout_gravity="center"
android:gravity="center"
android:background = "#drawable/picture_box"/>
</FrameLayout>
</LinearLayout>
<Button
android:id="#+id/mEditButton"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#drawable/edit_pic_button" />
<View
android:layout_width="match_parent"
android:layout_height="10dp" />
<Button
android:id="#+id/buttonUploadPic"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#drawable/upload_pic_button" />
</LinearLayout>
Use relative layout instead of linear layout and dont use hardcoded dimesions like 40dp,50dp if use app for different sizes of screen.
Relative layout
Problem in your layout is that you used fixed values in wrong places! Instead android:layout_width="100dp" in button (this in horizontal layout) you should use android:layout_width="fill_parent".
You have to set none zero value for layout_weight for this buttons so they could share space with other siblings in this layout.
Simplest way to convert fixed dp to weight:
change this:
android:layout_width="100dp"
to this:
android:layout_width="0dp"
android:layout_weight="100"
do it for all buttons and they will look exactly like they was in 4" and will scale perfect in any screen.

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