Android CardView example

A CardView is an android widget which is an extension of FrameLayout which can have rounded corners and shadows. It can wrap the other elements like TextView ,ImageView etc. into a single card which will give beautiful look and feel. In this tutorial we see an example for CardView in android. For using CardView in your application you need to compile the following library in your graddle.

compile 'com.android.support:cardview-v7:23.1.1'

Following is the code to be added to your layout for implementing CardView

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="2dp"
    app:cardUseCompatPadding="true"></android.support.v7.widget.CardView>
Now you can simply add the layout which you want to display inside the CardView, Here am using a LinearLayout which is containing one TextView and ImagView.
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="2dp"
card_view:contentPadding="10dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/im_language"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher" />


    <TextView
        android:id="@+id/tv_language"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="Title comes here" />
</LinearLayout>

</android.support.v7.widget.CardView>
Now Simply  Run the project you will get an output of card which contains an image and title.
In Next Tutorial we will learn How to implement listview using CardView

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

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