Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 825
Sony Pathanamthitta Avatar asked Mar 04 '15 06:03

Sony Pathanamthitta


4 Answers

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.

like image 185
the-ginger-geek Avatar answered Oct 19 '22 03:10

the-ginger-geek


you need severalimage for different displays
Supporting Multiple Screens

like image 31
gadfil Avatar answered Oct 19 '22 02:10

gadfil


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
like image 35
Dhrumil Shah - dhuma1981 Avatar answered Oct 19 '22 02:10

Dhrumil Shah - dhuma1981


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>
like image 1
Thien Huu Avatar answered Oct 19 '22 04:10

Thien Huu