How to make image fill RelativeLayout background without stretching - android

In my Android project, I am not quite sure how to make my background image fill the entirety of the RelativeLayout root element in XML, which is the size of the screen. I want to be sure that this works for all aspect ratios, so the image will clip vertically or horizontally as necessary. Does someone know how to do this easily? I've only seen questions regarding ImageViews and Buttons, but not really generic Views.
My XML file currently:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/enclosing_rl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:fitsSystemWindows="false">
<!-- Other elements -->
</RelativeLayout>

Other than turning your image into a nine patch I don't think this is possible. What you could do instead is-
Add an ImageView as the first view in your RelativeLayout.
Set layout_centerInParent to true.
Have the layout_width and layout_height set to match_parent.
Then set scaleType to centerCrop.
That will make sure the image fills the screen without any distortion, but depending on screen size/orientation either some of the top/bottom or left/right of the image may be cut off.
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="#drawable/background" />
Any other views in the RelativeLayout will appear on top of the ImageView, as long as it is the first view in the RelativeLayout (when you are in the xml).

Create a bitmap drawable XML resource in your res/drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:tileMode="repeat" />
Use that drawable as background instead of #drawable/background

according to this answer If you want your ImageView fill your RelativeLayout,use align parameters for ImageView.
you can put all of your views to a LinearLayout and set align parameter to your background ImageView:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/my_background"
android:scaleType="centerCrop"
android:layout_alignTop="#+id/my_views"
android:layout_alignBottom="#+id/my_views"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/my_views"
android:orientation="vertical" >
<!-- stuff -->
</LinearLayout>
</RelativeLayout>

Its smart and 100% working answer is to set the property scaleType of image view !
android:scaleType="fitCenter"

Related

Image size - how to?

I just want to make a simple image view but when I run that app the unnecessary white background comes. I don't want to need that part. any solution.
and here is my xml file for that image view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#drawable/pot1"/>
</LinearLayout>
In code use -
imgview.setScaleType(ScaleType.FIT_XY);
Or in xml Image view -
android:scaleType="fitXY"
remove the gravity attribute if you want to make it stick to the top of screen or add scaleType attribute fitCenter, cropCenter or fitXY if you want to make it fill the screen.

How to adjust Layout backgroud image to centerInside?

When assigning an image as a background to a Layout(Linear || Relative) it stretched all over the Layout, but I want to make that background image to be centered inside the Layout as in the ImageButtons :
android:scaleType="centerInside"
Is there's a way to achieve that in Linear or Relative Layouts?
You should try the....
1.) take the RelativeLayout propery matchParent both. and centerInparent=true
2.) take one ImageView inside it Property WrapContent.
now take the background or src of inage view
you can use another layout LayoutB inside the LayoutA. and set the width and height to wrap_content of LayoutB..and set the image to background of LayoutB..and also set the arributs like this:
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
This will help you
<?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" >
<ImageView
android:id="#+id/someImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/someImageSrc" />
</RelativeLayout>

Android activity border that can not be removed?

So I have this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bgdark"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="76dp"
android:src="#drawable/myheader_250x60"
/>
<ListView
android:id="#+id/mainListview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
Note I have now edited this post. I was using a grey background image, so I thought that was what was visible to right/left of header image.See below for an image showing the problem.
However, moving the background to the listview did not solve the problem. Hence it would seem it is the imageview that somehow has a left/right border in grey. As i have set width to "match_parent" it should have taken he maximum width.
The problem is that to the top/right of header there's a grey pixel border? which I would like to remove if possible.
Try scaleType attribute for your ImageView.
Anothrer way - replace
android:src="#drawable/myheader_250x60"
for
android:background="#drawable/myheader_250x60"
Try using fill_parent instead of match_parent except in listView height . Use wrap_content in listView height
set this scletype property
android:scaleType=fitXY
and set image to imageview like this
android:background="#drawable/myheader_250x60"

ImageView layout

Essentially I'm looking for something like scaleType="centerCrop" without the center.
I am having trouble making an ImageView display correctly when it's larger than the screen dimensions. I'm trying to display an ImageView that starts top=0, left=0 and is not scaled. It's okay that the image does not fit on the screen. Right now I have it in a relative layout:
<?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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/postagram_blank_large"/>
</RelativeLayout>
I've tried using a wrap_content on the RelativeLayout height and width. I've also played with scaleType as well as using a Scrollview. The problem with the ScrollView is that the image is both taller and wider than the display port (again this is meant to happen).
How can I make this work?
could it be that android:background scales the image to the screensize? try to use android:src instead.
Try this:
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="#drawable/postagram_blank_large"
android:scaleType="matrix"
/>
wrap_content instead of fill_parent also works, use whatever you want

Android. ImageView won't position itself correctly

I'm trying to place an image that fills the width of the screen, has the height of the actual image (wrap_content) and sits at the very bottom of the Layout.
I've wrote the below code, but between the second ImageView (the one with drawable/user_board) and the very bottom of the screen, there is a small space. Why is this? I've tried setting padding and margins to 0dp, but it seems it doesn't do the trick. Any ideas?
Here is the code:
<?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="fill_parent"
android:id="#+id/LayoutBoard"
android:noHistory="true"
android:padding="0dp" >
<ImageView
android:src="#drawable/game_interface_background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix" />
<ImageView
android:src="#drawable/user_board"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="0dp"
android:padding="0dp" />
</RelativeLayout>
Doesn't your user_board image have some transparent space at the bottom? Or maybe you can try setting negative value in the padding field of the second ImageView.
Use android:scaleType="fitXY" and the bitmap contained in your ImageView will be resized to fit the whole ImageView. So using this attribute in both your imageViews will probably hide that empty space. However keep in mind that this does not keep the aspect ratio of the image. See here for more information about the android:scaleType attribute tag and the possible values it can take.

Categories

Resources