Skip to main content

Android CardView Example

activity_main.xml

<?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:padding="16dp"
    tools:context=".MainActivity"
    android:background="#e7f2f9"
    >
    <!--
        CardView
            A FrameLayout with a rounded corner background and shadow.
            CardView uses elevation property on L for shadows and falls back to a custom shadow
            implementation on older platforms.
    -->
    <!--
        android.support.v7.cardview:cardElevation
            Elevation for CardView.
            Must be a dimension value, which is a floating point number appended with a unit such
            as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp
            (scaled pixels based on preferred font size), in (inches), mm (millimeters).

            This may also be a reference to a resource (in the form "@[package:]type:name") or
            theme attribute (in the form "?[package:][type:]name") containing a value of this type.

            This is a private symbol.
    -->
    <!--
        android.support.v7.cardview:contentPadding
            Inner padding between the edges of the Card and children of the CardView.
    -->
    <!--
        android.support.v7.cardview:cardBackgroundColor
            Background color for CardView.
            Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
            This may also be a reference to a resource (in the form "@[package:]type:name") or
            theme attribute (in the form "?[package:][type:]name") containing a value of this type.
            This is a private symbol.
    -->
    <!--
        android.support.v7.cardview:cardCornerRadius
            Corner radius for CardView.
            Must be a dimension value.
    -->
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        card_view:cardCornerRadius="5dp"
        card_view:contentPadding="25dp"
        card_view:cardBackgroundColor="#d2e0f1"
        card_view:cardElevation="5dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SAMPLE\nCARDVIEW"
            android:textSize="30sp"
            android:textStyle="bold"
            android:layout_gravity="center"
            android:gravity="center"
            android:textColor="#2a52a9"
            />
    </android.support.v7.widget.CardView>
</RelativeLayout>
build.gradle [dependencies]

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

Popular posts from this blog

Android Popup Window Example

activity_main.xml <?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:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity" android:background="#f5f1e0" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Popup Window" /> </RelativeLayout> res/layout/custom_layout.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_custom_layout" …

How to add a hint to Spinner in Android

activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity" android:background="#60c4ff" > <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> spinner_item.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textStyle="italic" …

How to create a round/circle Button in Android

activity_main.xml code snippet <Button android:id ="@+id/push_button" android:layout_width="150dp" android:layout_height="150dp" android:text="Round Button" android:background="@drawable/button_bg_round" android:padding="15dp" /> res/drawable/button_bg_round.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <stroke android:color="#1E90FF" android:width="5dp" /> <solid android:color="#87CEEB"/> <size android:width="150dp" android:height="150dp"/> </shape> </item> </selector> More android examples How to set Button fontHow to change Button font programmaticallyHow to set Button text style to bold and italic…