How to set background color on button click in Android || Back color change using Buttons - Android Tutorial World

Latest

Welcome to Android Tutorial World Blog, Here you can find all type of post or tutorial related to andorid app development in details.

Friday, September 13, 2019

How to set background color on button click in Android || Back color change using Buttons

How to change the background color on Button click


Hello friends, Today we will teach you how to change the layout background color on the Button click

Follow the Steps

Step 1. Create new project

Step 2. Create a Button in XML

Step 3. Now Set the id of your Main Layout and Button. Because we will change the background color of  our main layout

activity_main.xml:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_container"
    tools:context=".MainActivity">

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

</RelativeLayout>

Step 4. Find Button and Layout id in Java File and set OnClickListener on Button. Now We need to create a boolean variable to return true and false. ( Like :- boolean isColor = true;).This boolean variable checks the color value and pass true and false in return.

package com.akshayarora.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button button;
    RelativeLayout layout;
    boolean isColor = true;

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

        button = (Button) findViewById(R.id.btn);
        layout = (RelativeLayout)findViewById(R.id.main_container);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isColor) {
                    layout.setBackgroundColor(Color.BLUE);
                    isColor = false;
                }else {
                    layout.setBackgroundColor(Color.WHITE);
                    isColor = true;
                }
            }
        });
    }
}

We set the if statement for changing the Color on Button Click.
On the First click, its changes the color to Blue, and on the next click it will change to white color

See the Output:-

Run the app





Now click on the Button and see the output:-


Click again :-



Enjoy!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.