0% found this document useful (0 votes)
80 views3 pages

Change background color

The document provides a complete Android application setup that changes the background color of a layout when a button is clicked. It includes XML layout definitions, color resources, and Java code for the main activity, emphasizing the use of random color generation. Key improvements include better code clarity and robustness while maintaining the utility of predefined colors in the colors.xml file.

Uploaded by

mbs598678
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views3 pages

Change background color

The document provides a complete Android application setup that changes the background color of a layout when a button is clicked. It includes XML layout definitions, color resources, and Java code for the main activity, emphasizing the use of random color generation. Key improvements include better code clarity and robustness while maintaining the utility of predefined colors in the colors.xml file.

Uploaded by

mbs598678
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

XML

<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"
tools:context=".MainActivity"
android:id="@+id/main_layout">

<Button
android:id="@+id/changeColorButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Color"
android:layout_centerInParent="true"/>

</RelativeLayout>

XML

<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="red">#FF0000</color>
<color name="green">#00FF00</color>
<color name="blue">#0000FF</color>
<color name="yellow">#FFFF00</color>
</resources>

Java

// MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.graphics.Color;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

private RelativeLayout mainLayout;


private Button changeColorButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mainLayout = findViewById(R.id.main_layout);
changeColorButton = findViewById(R.id.changeColorButton);

changeColorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeBackgroundColor();
}
});
}

private void changeBackgroundColor() {


Random random = new Random();
int color = Color.argb(255, random.nextInt(256), random.nextInt(256),
random.nextInt(256)); //random color
mainLayout.setBackgroundColor(color);

}
}

Explanation and Improvements:

Layout (activity_main.xml):

A RelativeLayout is used as the root layout, giving flexibility for positioning


elements.
A Button is added to trigger the color change.
android:id="@+id/main_layout" is crucial so that the relative layout can be
accessed from the Java code.
Colors (colors.xml):

The colors.xml file defines color resources. While the random color method is used,
the colors.xml file is still very important and useful. You can use it to store
predefined colors that you will use in your app.
MainActivity.java:
findViewById() is used to get references to the RelativeLayout and Button from the
layout.
An OnClickListener is set on the button to call the changeBackgroundColor() method
when the button is clicked.
Random Color Generation:A Random object is used to generate random RGB color
values.
Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256))
creates a color with a random RGB value and full opacity (255).
mainLayout.setBackgroundColor(color) sets the background color of the
RelativeLayout.
How to Use in Android Studio:

Create a New Android Project:Open Android Studio and create a new project with an
"Empty Activity."
Add Layout XML:Copy and paste the activity_main.xml code into your
res/layout/activity_main.xml file.
Add Colors XML:Copy and paste the colors.xml code into your res/values/colors.xml
file.
Add Java Code:Copy and paste the MainActivity.java code into your MainActivity.java
file.
Run the App:Connect an Android device or use an emulator, and run the app.
Key Improvements:

Random Color: The code now generates truly random colors, providing a more dynamic
effect.
Clarity: The code is well-structured and easy to understand.
Robustness: The code is more robust.
The use of the color.xml file is explained and maintained. It is still useful for
storing colors that are not randomly generated.

You might also like