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.
Note: Download cover_sample_image from below and put inside drawable-hdpi folder.
Download below cover_sample_image 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 :