All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Set Background Image In Android XML

Last Updated : Mar 11, 2024

How To Set Background Image In Android XML

In this article we will show you the solution of how to set background image in android xml, any programme may be made to look more appealing by using background images.

As a result, most social media platforms, including WhatsApp and Messenger, provide this as a feature to its users.

We'll be developing a Android application that will enable users to change their background image by just pressing a button in light of this.

A simple application can be created by displaying a button and using the button's clicking event to modify the background pictures of the application.

Remember that we will construct an array of images saved on the inside of a drawable folder, then use the Random class to randomly fetch those photos.

Step By Step Guide On How To Set Background Image In Android XML :-

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.core.content.ContextCompat;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
    Button button;
    View screenView;
    int[] back_images;
    protected void onCreate(Bundle savedInstanceState) {
        // array creation of images which are stored
        // in drawable folder under res folder
        back_images = new int[]{R.drawable.geeksforgeeks, R.drawable.geeksforgeeks2,
                                R.drawable.geeksforgeeks3, R.drawable.geeksforgeeks4};
        button = findViewById(;
        screenView = findViewById(;
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // fetching length of array
                int array_length = back_images.length;
                // object creation of random class
                Random random = new Random();
                // generation of random number
                int random_number = random.nextInt(array_length);
                // set background images on screenView
                // using setBackground() method.
              screenView.setBackground(ContextCompat.getDrawable(getApplicationContext(), back_images[random_number]));


<?xml version="1.0" encoding="utf-8"?>
    <!--Button to perform clicking event
        to change background images-->
        android:text="Click Here"
        android:textColor="#FFFFFF" />
  1. The first method accomplishes the task quickly, but it is not the most efficient method, as I will explain later.
  2. Simply add the following code to your activity's layout XML file to get started:
  3. You can create a backdrop using any image saved in your app's drawable folder, where the background is the name of the image.
  4. android:background="@drawable/background"
  5. You can better comprehend the procedure by referring to the following pictures:
  6. Make sure your app's drawable folder contains the background image you want to use.
  7. Put the following code in the parent layout of your activity's XML layout: Use android:background=@drawable/background instead of android:background=@drawable/background where the background is the name of the image in your app's drawable folder that you wish to use as the background.
  8. The background has been modified!

Conclusion :-

I hope this article on how to set background image in android xml helps you and the steps and method mentioned above are easy to follow and implement.