Change CardView background color in android programmatically

How to set-apply different color on android.support.v7.widget.cardview widget on button click dynamically.

In this tutorial we are going to change the CardView widget background color on button click with completely dynamic method using MainActivity.java programming file. We are using setCardBackgroundColor() method to set color dynamically on CardView. So here is the complete step by step tutorial for Change CardView background color in android programmatically.

android-project-download-code-button

Note: Read below steps very carefully to add CardView library inside your current project.

1. Open your project’s build.gradle ( Module : app ) file.

build-gradle

2. Please add below code inside your build.gradle ( Module : app ) file.

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.0.+'
compile 'com.android.support:recyclerview-v7:23.0.+'

3. Screenshot of build.gradle ( Module : app ) file after adding above code.

Add Import CardView RecyclerView support library in Android Studio

Here your go now CardView library is successfully imported inside your project now its time to start coding.

How to Change CardView background color in android programmatically

Code for MainActivity.java file.

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

public class MainActivity extends AppCompatActivity {

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

        cardview = (CardView)findViewById(R.id.card_view1);
        button = (Button)findViewById(R.id.button);

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

                cardview.setCardBackgroundColor(Color.parseColor("#b70505"));

            }
        });
    }
}

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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.cardview_background_android_examplescom.MainActivity">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="6dp"
        card_view:cardElevation="6dp"
        card_view:contentPadding="25dp"
        card_view:cardBackgroundColor="#2196F3"

        android:layout_marginTop="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="SIMPLE \n CARDVIEW"
            android:textSize="30sp"
            android:textStyle="bold"
            android:textColor="#E3F2FD"
            />
    </android.support.v7.widget.CardView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to Change CardView background color in android programmatically"
        android:id="@+id/button"
        android:layout_below="@+id/card_view1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp" />

</RelativeLayout>

Screenshots:

Change CardView background color in android programmatically

cardview-background-2

Click here to download Change CardView background color in android programmatically project source code.

Leave a Reply

Your email address will not be published. Required fields are marked *