Jun 13, 2021

Open Another Activity Click On Card View In Activity - Androidpro.in

In this post we will learn how to open new activity click on card view in android studio, when I press the card view to go into another Activity.

Step 1  - Create a new project

Step 2 -  Add two dependencies

Step 2 - Create two new Activity which you want to open as new Activity.

Step 3 - Paste Xml code in ActivityMain.xml  which code provide in post.

Step 4 - Paste Java code in Main Activity which provide in post.

Step 5 - Your project is ready to “open new activity by click on card view in android studio”

Step 6 – Now run your project “I hope your project is successfully  created”.

 

Add In Your xml file activity main.xml

<androidx.cardview.widget.CardView
        android:id="@+id/bankingcard"
        android:layout_width="160dp"
        android:layout_height="190dp"
        android:layout_margin="10dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground">
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
 
            <ImageView
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:background="#7c4dff"
                android:padding="10dp"
                android:src="@drawable/download_5"
                />
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Banking"
                android:textStyle="bold" />
 
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_margin="10dp"
                android:background="#ededed" />
 
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="5dp"
                android:text="Check Your Bank Activities"
                android:textColor="@color/black" />
 
 
        </LinearLayout>
 
 
    </androidx.cardview.widget.CardView>
 
 

 

Add In Your java file activity main.java

    CardView bankingcard   
 bankingcard.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent(getApplicationContext(),MainActivity2.class);
               startActivity(intent);
           }
       });
 
    
 
 

 

 

 

 

Step 2

Add two dependencies

implementation "androidx.cardview:cardview:1.0.0"
implementation 'androidx.recyclerview:recyclerview:1.1.0'

 

Step 3

Step 3 - Paste Xml code in ActivityMain.xml  which code provide bellow.

ActivityMain.                                  Xml Code

Step – 1 Add this java code in the main activity.java

 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity">
 
    <androidx.cardview.widget.CardView
        android:id="@+id/bankingcard"
        android:layout_width="160dp"
        android:layout_height="190dp"
        android:layout_margin="10dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground">
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
 
            <ImageView
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:background="#7c4dff"
                android:padding="10dp"
                android:src="@drawable/download_5"
                />
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Banking"
                android:textStyle="bold" />
 
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_margin="10dp"
                android:background="#ededed" />
 
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="5dp"
                android:text="Check Your Bank Activities"
                android:textColor="@color/black" />
 
 
        </LinearLayout>
 
 
    </androidx.cardview.widget.CardView>
 
 
    <androidx.cardview.widget.CardView
        android:id="@+id/ideascard"
        android:layout_width="160dp"
        android:layout_height="190dp"
        android:layout_margin="10dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground">
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
 
            <ImageView
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:background="#ff4081"
                android:padding="10dp"
               android:src="@drawable/download_5" />
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Ideas"
                android:textStyle="bold" />
 
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_margin="10dp"
                android:background="#ededed" />
 
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="5dp"
                android:text="Check Your Bank "
                android:textColor="@color/black" />
 
 
        </LinearLayout>
 
 
    </androidx.cardview.widget.CardView>
 
</LinearLayout>
 

 

Step 4

Step 4 - Paste Java code in Main Activity which provide in post.

ActivityMain.java              Code

 

public class MainActivity extends AppCompatActivity {
 CardView bankingcard, ideascard;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        bankingcard = findViewById(R.id.bankingcard);
       bankingcard.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent(getApplicationContext(),MainActivity2.class);
               startActivity(intent);
           }
       });
 
        ideascard = findViewById(R.id.ideascard);
        ideascard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),MainActivity3.class);
                startActivity(intent);
            }
        });
 
    }
}
 

 

 

Step 6

Step 6 – Now run your project “I hope your project is successfully  created” and output will like this.

 

Note – If you get any error comment in comment box. 

 

Whatsapp Button works on Mobile Device only