Sony Pathanamthitta
Sony Pathanamthitta

Reputation: 223

Android Background image fit screen without stretching

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>

Upvotes: 6

Views: 20865

Answers (5)

Himanshu Likhyani
Himanshu Likhyani

Reputation: 4570

If using a PNG/JPG image as the background (and not xml), create another XML drawable, and set THAT as your background:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center"
    android:src="@drawable/my_png_jpg_image" />

Upvotes: 0

Thien Huu
Thien Huu

Reputation: 36

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>

Upvotes: 1

Dhrumil Shah - dhuma1981
Dhrumil Shah - dhuma1981

Reputation: 15789

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

Upvotes: 1

gadfil
gadfil

Reputation: 417

you need severalimage for different displays
Supporting Multiple Screens

Upvotes: 1

the-ginger-geek
the-ginger-geek

Reputation: 7081

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.

Upvotes: 12

Related Questions