May 15, 2021

CardView With ImageView, TextView Android Studio - Androidpro.in

Open New Activity By Click On Card View In Activity In Android Studio.

 


How to open new Activity by clicking [card view] in activity.  I am beginner and i have no idea of using [Card View] as a button. Help me i need example code.

Here I Add Two card and open two New Activity for open new view.

 

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”.

 

Step 2

Add two dependencies

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

Step 3

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

ActivityMain.                                  Xml Code

 

<?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 err comment in comment box. 


Whatsapp Button works on Mobile Device only