All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - 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.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
    Button button;
    View screenView;
    int[] back_images;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 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(R.id.Button);
        screenView = findViewById(R.id.relative_layout);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <!--Button to perform clicking event
        to change background images-->
    <Button
        android:id="@+id/Button"
        android:layout_width="150dp"
        android:layout_height="52dp"
        android:layout_margin="12dp"
        android:background="#0F9D58"
        android:text="Click Here"
        android:textColor="#FFFFFF" />
</RelativeLayout>
  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.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪