Stretch background image in RelativeLayout android using xml

How to stretch to completely fill cover RelativeLayout with single drawable resource background image.

Stretching means automatically increase image width plus height according to destination layout so if main image object is small then it will automatically increase its size and stretch itself same as layout size and cover the whole layout. So we can do this by using  android:layout_width=”fill_parent”
android:layout_height=”fill_parent” attributes so they will automatically fill up the whole layout with defined background image. So here is the complete step by step tutorial for Stretch background image in RelativeLayout android using xml.

android-project-download-code-button

Note: Download cover_sample_image from below and put inside drawable-hdpi folder.

drawable hdpi folder

Download below cover_sample_image image.

cover_sample_image

How to Stretch background image in RelativeLayout android using xml.

Code for MainActivity.java file.

package com.stretchbackgroundimagerelativelayout_android_examples.com;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 tools:context="com.stretchbackgroundimagerelativelayout_android_examples.com.MainActivity"
 android:background="@drawable/cover_sample_image" >

</RelativeLayout>

Screenshot :

Stretch background image in RelativeLayout android using xml

Click here to download Stretch background image in RelativeLayout android using xml project.

Leave a Reply

Your email address will not be published. Required fields are marked *