6

I have developed an android application. I used android:background property to make an image as background. But when it is displayed on square displays, the image keeps stretching. How to make Android Background fit the screen without any stretching ? I have seen many apps with images or videos that are non stretched , but fills the entire screen of the android phone.

My xml code is given below

<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" tools:context=".MainActivity"
    android:background="@drawable/Splash" >

</RelativeLayout>
Sony Pathanamthitta
  • 223
  • 1
  • 4
  • 11
  • It depends on your image. How it looks? Because `android:background="@drawable/Splash"` will cover whole screen and fit to screen. – Piyush Mar 04 '15 at 06:07
  • Actually its a 1536x2048 image with lot of devices image like in the splash screen of adt/androidstudio. I have seen many apps that fits the background with an image , but not stretched. I cannot post images here. because my reputation in below 10 – Sony Pathanamthitta Mar 04 '15 at 06:09
  • Have a look at my answer and see if it helps – the-ginger-geek Mar 06 '15 at 10:33

4 Answers4

12

You can try and use an ImageView rather than setting it to the background of the RelativeLayout

I have modified your code to look like this

<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" tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/Splash"/>
</RelativeLayout>

Only problem with this solution is that the image might be cut off at parts on some devices. To solve this issue you can create a few images with different aspect ratios for relevant devices and put them into the relevant drawable-xxxx folders.

Also have a look at this thread for other solutions that might suit your requirements better.

Community
  • 1
  • 1
the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45
1

you need severalimage for different displays
Supporting Multiple Screens

gadfil
  • 417
  • 4
  • 13
1

In Android to support multiple screen you must have to add different images according to screen resolution and screen size.

For more details just visit these links:

  1. Drawable folders in res folder?
  2. http://developer.android.com/guide/practices/screens_support.html
Community
  • 1
  • 1
Dhrumil Shah - dhuma1981
  • 15,166
  • 6
  • 31
  • 39
1

You could use a FrameLayout and put ImageView as background

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imgPlaylistItemBg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:maxHeight="0dp"
        android:scaleType="fitXY"
        android:src="@drawable/img_dsh" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

    </LinearLayout>

</FrameLayout>
Saravanan Sachi
  • 2,572
  • 5
  • 33
  • 42
Thien Huu
  • 36
  • 3