Sunday, 14 September 2025

Just Tap to Change Color!

Here we will learn the easiest and shortest way to change color of a simple app with just a tap.

     

XML layout:

In the TextView, change the text 'Hello World!' to 'Just Tap', increase its size and make it bold:

android:text="JUST TAP!"
android:textSize="45sp"
android:textStyle="bold"
android:textColor="#ffff00"

Now our activity_main.xml has only this much code. We already have the id set to the Constraint Layout which is android:id="@+id/main"

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JUST TAP!"
android:textSize="45sp"
android:textStyle="bold"
android:textColor="#ffff00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Now in the MainActivity.java, use the above mentioned id to set a new color:

private ConstraintLayout mainLayout;

Create an array of various colors that we want to appear randomly when we touch the screen:

private int colors[] = {Color.BLUE, Color.BLACK, Color.CYAN, Color.DKGRAY, Color.WHITE, Color.GREEN,
Color.YELLOW, Color.TRANSPARENT, Color.MAGENTA, Color.RED, Color.LTGRAY, Color.GRAY};

Create a Random object to get a random color:

private Random random = new Random();

In the OnCreate method, assign the value to mainLayout:

mainLayout = findViewById(R.id.main);

Finally override the OnUserInteraction method and change the background in this way:

mainLayout.setBackgroundColor(colors[random.nextInt(colors.length - 1)]);

So the full and complete code of MainActivity.java will be:

package com.prashantsj.apps.colorchanger;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

private ConstraintLayout mainLayout;
private int colors[] = {Color.BLUE, Color.BLACK, Color.CYAN, Color.DKGRAY, Color.WHITE, Color.GREEN,
Color.YELLOW, Color.TRANSPARENT, Color.MAGENTA, Color.RED, Color.LTGRAY, Color.GRAY};
private Random random = new Random();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
mainLayout = findViewById(R.id.main);
}

@Override
public void onUserInteraction() {
super.onUserInteraction();

mainLayout.setBackgroundColor(colors[random.nextInt(colors.length - 1)]);
}
}




No comments:

Post a Comment