Professional Documents
Culture Documents
Class Overview
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.
Due to expensive nature of rounded corner clipping, on platforms before L, CardView does not
clip its children that intersect with rounded corners. Instead, it adds padding to avoid such
intersection (See setPreventCornerOverlap(boolean) to change this behavior).
Before L, CardView adds padding to its content and draws shadows to that area. This padding
amount is equal to maxCardElevation + (1 - cos45) * cornerRadius on the sides
and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom.
Since padding is used to offset content for shadows, you cannot set padding on CardView.
Instead, you can use content padding attributes in XML orsetContentPadding(int, int,
int, int) in code to set the padding between the edges of the Card and children of CardView.
Note that, if you specify exact dimensions for the CardView, because of the shadows, its content
area will be different between platforms before L and after L. By using api version specific
resource values, you can avoid these changes. Alternatively, If you want CardView to add inner
padding on platforms L and after as well, you can
set setUseCompatPadding(boolean) to true.
To change CardView's elevation in a backward compatible way,
use setCardElevation(float). CardView will use elevation API on L and before L, it will
change the shadow size. To avoid moving the View while shadow size is changing, shadow size
is clamped by getMaxCardElevation(). If you want to change elevation dynamically, you
should call setMaxCardElevation(float) when CardView is initialized.
Create Cards
CardView extends the FrameLayout class and lets you show information inside cards that have
a consistent look across the platform. CardView widgets can have shadows and rounded
corners.
To create a card with a shadow, use the card_view:cardElevationattribute. CardView uses real
elevation and dynamic shadows on Android 5.0 (API level 21) and above and falls back to a
programmatic shadow implementation on earlier versions. For more information, seeMaintaining
Compatibility.
Use these properties to customize the appearance of the CardViewwidget:
The following code example shows you how to include a CardView widget in your layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
... >
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.CardView>
</LinearLayout>
Add Dependencies
The RecyclerView and CardView widgets are part of the v7 Support Libraries. To use these
widgets in your project, add these Gradle dependencies to your app's module:
dependencies {
...
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
}
(Global Positioning System). The accuracy of the location is determined by the providers, the
location permissions you've requested, and the options you set in the location request.
This lesson shows you how to request regular updates about a device's location using
therequestLocationUpdates() method in the fused location provider.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.gms.location.sample.locationupdates" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>
Update interval
setInterval() - This method sets the rate in milliseconds at which your app prefers to
receive location updates. Note that the location updates may be faster than this rate if
another app is receiving updates at a faster rate, or slower than this rate, or there may be
no updates at all (if the device has no connectivity, for example).
Fastest update interval
setFastestInterval() - This method sets the fastest rate in milliseconds at which your
app can handle location updates. You need to set this rate because other apps also
affect the rate at which updates are sent. The Google Play services location APIs send
out updates at the fastest rate that any app has requested with setInterval(). If this rate
is faster than your app can handle, you may encounter problems with UI flicker or data
overflow. To prevent this, call setFastestInterval() to set an upper limit to the update
rate.
Priority
setPriority() - This method sets the priority of the request, which gives the Google Play
services location services a strong hint about which location sources to use. The
following values are supported:
possible. With this setting, the location services are more likely to use GPS (Global
Positioning System) to determine the location.
consumption, but want to receive location updates when available. With this setting,
your app does not trigger any location updates, but receives locations triggered by
other apps.