Change background color
Change background color
<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;
@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();
}
});
}
}
}
Layout (activity_main.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.