Create bitmap using external xml to set image inside bitmap drawable then add bitmap as relativelayout background.
In this tutorial we are creating Bitmap using another layout xml file with Bitmap tag and inside Bitmap tag we are setting up background image src path. Now we are setting this particular xml to RelativeLayout background. So we are here automatically converts normal drawable image to bitmap image using out source xml. So here is the complete step by step tutorial for Create/Set bitmap to relativelayout background in android.
Note: Download below bitmap_sample.jpg image and put inside drawable-hdpi folder.
How to Create/Set bitmap to relativelayout background in android.
Code for MainActivity.java file.
package com.bitmap2relativelayoutbackground_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="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bitmap2relativelayoutbackground_android_examples.com.MainActivity"
android:background="@layout/bitmap_create" >
</RelativeLayout>
Code for bitmap_create.xml file.
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/bitmap_sample"
android:tileMode="clamp"
/>
Screenshot: