Android Generate Random Color on Button Click Programmatically

In this tutorial we would going to change – set the background color of a View dynamically on button click. The random color would generate using JAVA’S default Random method. This is a pre inbuilt method available in JAVA. So here is the complete step by step tutorial for Android Generate Random Color on Button Click Programmatically.

Contents in this project Generate Random Color on Button Click.

1. Create a Relative Layout view as Root view in your activity_main.xml file . This is the View which background color we would change on button click.

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.generaterandomcolor_android_examplescom.MainActivity"
    android:id="@+id/relativeLayout">

</RelativeLayout>

2. Create a button widget inside the root Relative layout view.

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.generaterandomcolor_android_examplescom.MainActivity"
    android:id="@+id/relativeLayout">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Generate Random Color" />

</RelativeLayout>

3. Create Relative Layout and Button objects in MainActivity.java file.

Button button;
RelativeLayout relativeLayout;

4. Assign ID’S to them .

button = (Button)findViewById(R.id.button);

relativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout);

5. Add onClickListener function on Button.

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        

    }
});

6. Create Random object and then create random color and set the color as background of View.

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        Random random = new Random();

        int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));

        relativeLayout.setBackgroundColor(color);

    }
});

 Complete Source Code :

Code for MainActivity.java file.

package com.android_examples.generaterandomcolor_android_examplescom;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

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

        button = (Button)findViewById(R.id.button);

        relativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Random random = new Random();

                int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));

                relativeLayout.setBackgroundColor(color);

            }
        });
    }
}

Code for activity_main.xml layout file.

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.generaterandomcolor_android_examplescom.MainActivity"
    android:id="@+id/relativeLayout">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Generate Random Color" />

</RelativeLayout>

Screenshots :

Generate Random Color