Card View Set On Click Listener - Androidpro.in

Xml Code

<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity">
 
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">
 
       <androidx.cardview.widget.CardView
           android:id="@+id/cardview1"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_margin="10dp">
 
           <RelativeLayout
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               >
               <ImageView
                   android:id="@+id/imageview1"
                   android:layout_width="match_parent"
                   android:layout_height="160dp"
                   android:src="@drawable/albert_einstein"
                   android:layout_margin="5dp"
                   />
               <TextView
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="CardView One"
                   android:layout_below="@id/imageview1"
                   android:textSize="26dp"
                   android:textStyle="bold"
                   android:textAlignment="center"
                   android:gravity="center_horizontal" />
 
           </RelativeLayout>
 
       </androidx.cardview.widget.CardView>

       <androidx.cardview.widget.CardView
           android:id="@+id/cardview2"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_margin="10dp">
 
           <RelativeLayout
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               >
               <ImageView
                   android:id="@+id/imageview2"
                   android:layout_width="match_parent"
                   android:layout_height="160dp"
                   android:src="@drawable/abe_lincoln"
                   android:layout_margin="5dp"
                   />
               <TextView
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="CardView Two"
                   android:layout_below="@id/imageview2"
                   android:textSize="26dp"
                   android:textStyle="bold"
                   android:textAlignment="center"
                   android:gravity="center_horizontal" />
 
           </RelativeLayout>
 
       </androidx.cardview.widget.CardView>
       <androidx.cardview.widget.CardView
           android:id="@+id/cardview3"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_margin="10dp">
 
           <RelativeLayout
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               >
               <ImageView
                   android:id="@+id/imageview3"
                   android:layout_width="match_parent"
                   android:layout_height="160dp"
                   android:src="@drawable/abe_lincoln"
                   android:layout_margin="5dp"
                   />
               <TextView
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="CardView Three"
                   android:layout_below="@id/imageview3"
                   android:textSize="26dp"
                   android:textStyle="bold"
                   android:textAlignment="center"
                   android:gravity="center_horizontal" />
 
           </RelativeLayout>
 
       </androidx.cardview.widget.CardView>
 
   </LinearLayout>
 
</androidx.core.widget.NestedScrollView>
 

 

Java Code

 
package com.app.androidpro.passiveincomeideas.ui.gallery;
 
 
 
package com.app.androidpro.myapplication;
 
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
 
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
 
public class MainActivity extends AppCompatActivity {
 
    private CardView cardView1,cardView2,cardView3;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        cardView1 = findViewById(R.id.cardview1);
        cardView2 = findViewById(R.id.cardview2);
        cardView3 = findViewById(R.id.cardview3);
 
        cardView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,MainActivity2.class);
                startActivity(intent);
            }
        });
 
        cardView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(MainActivity.this,MainActivity3.class);
                startActivity(intent1);
            }
        });
 
        cardView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(MainActivity.this,MainActivity4.class);
                startActivity(intent2);
            }
        });
    }
}
 

 

Now your project is ready