How to Change the Background Color After Clicking the Button in Android? Last Updated : 06 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will see how we can change the background of the screen by clicking a button. For this, we will be using the onClick() method. When we click on the button the onClick function is called. To set the click handler event for the button we need to define the android:onClick attribute in the XML file. We can also use onClickListener() in the Java file to call this function programmatically when the button is clicked. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. Step-by-Step ImplementationStep 1: Create a New ProjectTo create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.Step 2: Define ColorsIt is always better to pre-define strings and colors instead of hard coding them hence we will define the colors.Open the colors.xml file by navigating to the app -> res -> values -> colors.xmlCreate a color tag inside the resources tag with a name and set a color with its hex code.Add the below lines inside the colors.xml file. XML <color name="colorPrimary">#6200EE</color> <color name="colorPrimaryDark">#3700B3</color> <color name="colorAccent">#03DAC5</color> <color name="green">#0F9D58</color> <color name="cool">#188FCF</color> <color name="warm">#F1D416</color> Step 3: Working with the activity_main.xml fileGo to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file. 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/rlVar1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/green" tools:context=".MainActivity"> <TextView android:id="@+id/tvVar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="240dp" android:text="What would you like?" android:textSize="30dp" android:textStyle="bold" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tvVar1" android:layout_centerInParent="true" android:layout_marginTop="60dp" android:orientation="horizontal" android:padding="10dp"> <Button android:id="@+id/btVar1" android:layout_width="150dp" android:layout_height="wrap_content" android:padding="20dp" android:text="Cool" android:textSize="25dp" /> <Button android:id="@+id/btVar2" android:layout_width="150dp" android:layout_height="wrap_content" android:padding="20dp" android:text="Warm" android:textSize="25dp" /> </LinearLayout> </RelativeLayout> Step 4: Working with the MainActivity.java file Set onClick() attribute with a function name android:onClick="changeBackground",After that in your activity that hosts this layout create a function with the same name, orYou can instead of using the onClick() attribute directly set the onClickListener() and code its functionInside the function use setBackgroundResource(R.color.button_color) function, this will set the background with color button_color.Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail. Java import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1, button2; final RelativeLayout relativeLayout; // set button 1 with its id button1 = findViewById(R.id.btVar1); // set button 2 with its id button2 = findViewById(R.id.btVar2); // set relative layout with its id relativeLayout = findViewById(R.id.rlVar1); // onClick function for button 1 button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // set the color to relative layout relativeLayout.setBackgroundResource(R.color.cool); } }); // onClick function for button 2 button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // set the color to relative layout relativeLayout.setBackgroundResource(R.color.warm); } }); } } Output: Comment More infoAdvertise with us Next Article How to Change the Background Color After Clicking the Button in Android? N namanjha10 Follow Improve Article Tags : Java Android Android-Button Android Projects Practice Tags : Java Similar Reads How to Change Background Image by Button Clicking Event in Android? Background Images play an important role in the beautification of any application. Hence, most social media applications like WhatsApp, Messenger provides this as a part of their feature to their users. So, keeping this in mind we will be going to develop an android application in which background i 3 min read How to Change the Background Color of Button in Android using ColorStateList? ColorStateList is an object which can define in an XML file that can be used to apply different colors on widgets (such as Buttons , etc) depending on the state of Widgets to which it is being applied . For Example, There are many states of Buttons like (pressed, focussed, or none of them ) and othe 4 min read How to Change Background Color of ListView Items in Android? In Android, a ListView is a layout element that is used to display items in a list. This is the simplest form of displaying a list or array of items and one can choose from pre-developed layouts for displaying an element without creating a separate layout unlike in other similar views. Each item in 2 min read How to change the color of Action Bar in an Android App? Customizing the Action Bar allows you to enhance the visual appeal of your Android app. In this article, you will learn how to change the colour of the Action Bar in an Android App. Basically, there are two ways to change color.By changing styles.xml file:Just go to res/values/styles.xml fileedit th 2 min read How to Change the Color of Status Bar in an Android App? A Status Bar in Android is an eye-catching part of the screen, all of the notification indications, battery life, time, connection strength, and plenty of things are shown here. An Android user may look at a status bar multiple times while using an Android application. It is a very essential part of 4 min read How to change Input Method Action Button in Android? In this article, IME(Input Method Action) Option is changed in android according to our requirement. Input Method Action Button is located in the bottom right corner of the soft keyboard. By default, the system uses this button for either a Next or Done action unless your text field allows multi-lin 2 min read How to Change Colors of a Floating Action Button in Android? Android applications use the Floating Action Button for prompting the user to perform some important action within the android application. Floating Action Buttons in android applications are used to perform some important functionality within android applications. Many times in the android applicat 3 min read How to Change the ListView Text Color in Android? In Android, a ListView is a UI element used to display the list of items. This list is vertically scrollable and each item in the ListView is operable. A ListView adapter is used to supply items from the main code to the ListView in real-time. By default, the TextView font size is 14 sp and color is 3 min read How to Change the ProgressBar Color in Android? In this article, we will see how we can add color to a ProgressBar in android. Android ProgressBar is a user interface control that indicates the progress of an operation. For example, downloading a file, uploading a file on the internet we can see the ProgressBar estimate the time remaining in oper 3 min read How to Change Button Font in Android? A Button in Android is a UI element provided to a user to click to perform a certain action. A text can be set inside the button to name it. However, this text can be seen in a particular font only which is set by default. So in this article, we will show you how you could change the Button text fon 2 min read Like