Create CardView programmatically in android App on button click

How to add configure CardView attributes with TextView dynamically inside android app.

In this tutorial we are going to make CardView widget with TextView using complete dynamic method. We are declaring all the widgets elements( Attributes ) like setLayoutParams, setRadius, setPadding, setCardBackgroundColor, setMaxCardElevation, setMaxCardElevation, addView dynamically. So here is the complete step by step tutorial for Create CardView programmatically in android App on button click.

android-project-download-code-button

Note : Please follow all the below steps to add CardView library.

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 start coding.

How to Create CardView programmatically in android App on button click.

Code for MainActivity.java file.

package com.android_examples.cardviewprogrammatically_android_examplescom;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button button;
    Context context;
    CardView cardview;
    LayoutParams layoutparams;
    TextView textview;
    RelativeLayout relativeLayout;

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

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

        context = getApplicationContext();

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

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

                CreateCardViewProgrammatically();

            }
        });
    }

    public void CreateCardViewProgrammatically(){

        cardview = new CardView(context);

        layoutparams = new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
        );

        cardview.setLayoutParams(layoutparams);

        cardview.setRadius(15);

        cardview.setPadding(25, 25, 25, 25);

        cardview.setCardBackgroundColor(Color.MAGENTA);

        cardview.setMaxCardElevation(30);

        cardview.setMaxCardElevation(6);

        textview = new TextView(context);

        textview.setLayoutParams(layoutparams);

        textview.setText("CardView Programmatically");

        textview.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25);

        textview.setTextColor(Color.WHITE);

        textview.setPadding(25,25,25,25);

        textview.setGravity(Gravity.CENTER);

        cardview.addView(textview);

        relativeLayout.addView(cardview);

    }

}

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.cardviewprogrammatically_android_examplescom.MainActivity"
    android:id="@+id/relativelayout1">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to Create CardView programmatically on Button click"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Screenshots:

dynamic-cardview-1

 

Create CardView programmatically in android App on button click

Click here to download Create CardView programmatically in android App on button click project with source code.