Open In App

How to Set Background Drawable Programmatically in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

In many android applications, we can get to see that the background color of this application changes dynamically when updated from the server. For updating this color we have to set the background color of our layout programmatically. In this article, we will take a look at How to Set Background Drawable Programmatically in an Android application.  

Note: This Android article covered in both Java and Kotlin languages. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/idRLContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <!--on below line we are creating
        a text for heading of our app-->
    <TextView
        android:id="@+id/idTVHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="Background Drawable in Android"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold" />
 
</RelativeLayout>


Step 3: Creating a custom drawable

Navigate to app>res>drawable>Right click on it>New Drawable file and name it as back_drawable and add the below code to it. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
    android:shape="rectangle">
    <!--on below line we are adding gradient and
    specifying start and end color with angle-->
    <gradient
        android:angle="270"
        android:endColor="@color/white"
        android:startColor="#0F9D58" />
 
</shape>


Step 4: Working with the MainActivity file 

Navigate to app > java > your app’s package name > MainActivity file and add the code below. Comments are added in the code to get to know in detail. 

Kotlin




package com.gtappdevelopers.kotlingfgproject
 
import android.os.Bundle
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
 
class MainActivity : AppCompatActivity() {
 
    // on below line we are creating variable for our view.
    lateinit var containerRL: RelativeLayout
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // on below line we are initializing our variable with id.
        containerRL = findViewById(R.id.idRLContainer)
 
        // on below line we are setting background for
        // our relative layout on below line.
        containerRL.background = resources.getDrawable(R.drawable.back_drawable)
 
    }
}


Java




package com.gtappdevelopers.kotlingfgproject;
 
import android.os.Bundle;
import android.widget.RelativeLayout;
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
 
    // on the below line we are creating a variable.
    private RelativeLayout containerRL;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // on below line we are initializing variables with ids.
        containerRL = findViewById(R.id.idRLContainer);
 
        // on below line we are setting background for
        // our relative layout on below line.
        containerRL.setBackground(getResources().getDrawable(R.drawable.back_drawable));
    }
}


Now run your application to see the output of it. 

Output:

Output



Last Updated : 23 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads