Add elevation settings to cardview demo app
Bug: 16501750
Change-Id: I831f191c8db1864ba2f1dfa3af9ffbe4005b2e0c
diff --git a/samples/Support7Demos/res/layout/activity_card_view.xml b/samples/Support7Demos/res/layout/activity_card_view.xml
index f88daf7..cfd3071 100644
--- a/samples/Support7Demos/res/layout/activity_card_view.xml
+++ b/samples/Support7Demos/res/layout/activity_card_view.xml
@@ -28,6 +28,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
+ android:textColor="@android:color/black"
android:id="@+id/radius_label"
android:layout_marginBottom="5dp"
android:text="@string/card_view_radius"/>
@@ -42,6 +43,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
+ android:textColor="@android:color/black"
android:id="@+id/width_label"
android:text="@string/card_view_width"
android:layout_below="@id/radius_label"
@@ -57,6 +59,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
+ android:textColor="@android:color/black"
android:id="@+id/height_label"
android:text="@string/card_view_height"
android:layout_below="@id/width_label"
@@ -69,16 +72,98 @@
android:layout_toRightOf="@id/height_label"
android:layout_alignTop="@id/height_label"/>
- </RelativeLayout>
- <android.support.v7.widget.CardView
- 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="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginBottom="5dp"
+ android:id="@+id/elevation_label"
+ android:textColor="@android:color/black"
+ android:text="@string/card_view_elevation"
+ android:layout_below="@id/height_label"
+ android:layout_alignRight="@id/height_label"/>
+ <SeekBar
+ android:id="@+id/elevation_seek_bar"
+ android:max="50"
android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </android.support.v7.widget.CardView>
+ android:layout_height="wrap_content"
+ android:layout_toRightOf="@id/elevation_label"
+ android:layout_alignTop="@id/elevation_label"/>
+
+ <TextView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginBottom="5dp"
+ android:id="@+id/max_elevation_label"
+ android:textColor="@android:color/black"
+ android:text="@string/card_view_max_elevation"
+ android:layout_below="@id/elevation_label"
+ android:layout_alignRight="@id/elevation_label"/>
+ <SeekBar
+ android:id="@+id/max_elevation_seek_bar"
+ android:max="60"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_toRightOf="@id/max_elevation_label"
+ android:layout_alignTop="@id/max_elevation_label"/>
+
+ <TextView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginBottom="5dp"
+ android:id="@+id/alpha_label"
+ android:textColor="@android:color/black"
+ android:text="@string/card_view_alpha"
+ android:layout_below="@id/max_elevation_label"
+ android:layout_alignRight="@id/max_elevation_label"/>
+ <SeekBar
+ android:id="@+id/alpha_seek_bar"
+ android:max="255"
+ android:progress="255"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_toRightOf="@id/alpha_label"
+ android:layout_alignTop="@id/alpha_label"/>
+
+ <RadioGroup
+ android:id="@+id/select_target_radio"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:orientation="horizontal"
+ android:layout_below="@id/alpha_seek_bar">
+ <RadioButton
+ android:id="@+id/resize_card_view"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:textColor="@android:color/black"
+ android:text="@string/card_view_resize_card_view"
+ android:checked="true"/>
+ <RadioButton
+ android:id="@+id/resize_content"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:textColor="@android:color/black"
+ android:text="@string/card_view_resize_content"/>
+ </RadioGroup>
+
+ </RelativeLayout>
+ <RelativeLayout android:layout_width="fill_parent"
+ android:layout_height="match_parent">
+ <android.support.v7.widget.CardView
+ android:id="@+id/card_view"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ card_view:cardElevation="10dp">
+ <TextView
+ android:id="@+id/info_text"
+ android:textColor="@android:color/black"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent" />
+ </android.support.v7.widget.CardView>
+ <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:text="@string/card_view_sample_text"
+ android:textColor="@android:color/black"
+ android:layout_toRightOf="@id/card_view"
+ android:layout_alignTop="@id/card_view"/>
+ </RelativeLayout>
+
</LinearLayout>
diff --git a/samples/Support7Demos/res/values/strings.xml b/samples/Support7Demos/res/values/strings.xml
index 3ae6be7..8530f00 100644
--- a/samples/Support7Demos/res/values/strings.xml
+++ b/samples/Support7Demos/res/values/strings.xml
@@ -115,6 +115,12 @@
<string name="card_view_radius">Radius</string>
<string name="card_view_width">Width</string>
<string name="card_view_height">Height</string>
+ <string name="card_view_elevation">Elevation</string>
+ <string name="card_view_max_elevation">Max Elevation</string>
+ <string name="card_view_alpha">Alpha</string>
+ <string name="card_view_sample_text">Sample text on right</string>
+ <string name="card_view_resize_card_view">Resize CardView</string>
+ <string name="card_view_resize_content">Resize Content</string>
<string name="palette">Palette</string>
<string name="palette_all_colors">Full color palette</string>
diff --git a/samples/Support7Demos/src/com/example/android/supportv7/view/CardViewActivity.java b/samples/Support7Demos/src/com/example/android/supportv7/view/CardViewActivity.java
index e680aaa..ba4f0cb 100644
--- a/samples/Support7Demos/src/com/example/android/supportv7/view/CardViewActivity.java
+++ b/samples/Support7Demos/src/com/example/android/supportv7/view/CardViewActivity.java
@@ -18,9 +18,11 @@
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
+import android.support.v4.view.ViewCompat;
import android.support.v7.widget.CardView;
import android.view.View;
-import android.widget.LinearLayout;
+import android.view.ViewGroup;
+import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import com.example.android.supportv7.R;
@@ -37,6 +39,14 @@
SeekBar mHeightSeekBar;
+ SeekBar mElevationSeekBar;
+
+ SeekBar mMaxElevationSeekBar;
+
+ SeekBar mAlphaSeekBar;
+
+ boolean mResizeCardView = true;
+
private SeekBar.OnSeekBarChangeListener mOnSeekBarChangedListener
= new SeekBar.OnSeekBarChangeListener() {
@Override
@@ -56,13 +66,41 @@
};
private void update() {
- mCardView.setRadius(mCornerRadiusSeekBar.getProgress());
- LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mCardView.getLayoutParams();
+ mElevationSeekBar.setMax(mMaxElevationSeekBar.getProgress());
+ if (mCornerRadiusSeekBar.getProgress() != mCardView.getRadius()) {
+ mCardView.setRadius(mCornerRadiusSeekBar.getProgress());
+ }
+ if (mElevationSeekBar.getProgress() != mCardView.getCardElevation()) {
+ mCardView.setCardElevation(mElevationSeekBar.getProgress());
+ }
+ if (mMaxElevationSeekBar.getProgress() != mCardView.getMaxCardElevation()) {
+ mCardView.setMaxCardElevation(mMaxElevationSeekBar.getProgress());
+ }
+ ViewCompat.setAlpha(mCardView, mAlphaSeekBar.getProgress() / 255f);
+ ViewGroup.LayoutParams lp;
+ if (mResizeCardView) {
+ lp = setViewBounds(mCardView);
+ } else {
+ lp = setViewBounds(mInfoText);
+ }
+ mInfoText.setText("radius: " + mCornerRadiusSeekBar.getProgress()
+ +", alpha: " + mAlphaSeekBar.getProgress()
+ + "\n w: " + lp.width + "\nh: " + lp.height
+ + "\nelevation: " + mCardView.getCardElevation() + " of "
+ + mCardView.getMaxCardElevation());
+ }
+
+ private ViewGroup.LayoutParams setViewBounds(View view) {
+ ViewGroup.LayoutParams lp = view.getLayoutParams();
+ boolean changed = lp.width != mWidthSeekBar.getProgress()
+ || lp.height != mHeightSeekBar.getProgress();
+ if (!changed) {
+ return lp;
+ }
lp.width = mWidthSeekBar.getProgress();
lp.height = mHeightSeekBar.getProgress();
- mCardView.setLayoutParams(lp);
- mInfoText.setText("radius : " + mCornerRadiusSeekBar.getProgress()
- + "\n w:" + lp.width + "\nh:" + lp.height);
+ view.setLayoutParams(lp);
+ return lp;
}
@Override
@@ -84,6 +122,27 @@
mHeightSeekBar.setProgress(mCardView.getLayoutParams().height);
mHeightSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);
+ mElevationSeekBar = (SeekBar) findViewById(R.id.elevation_seek_bar);
+ mElevationSeekBar.setProgress((int) mCardView.getCardElevation());
+ mElevationSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);
+
+ mMaxElevationSeekBar = (SeekBar) findViewById(R.id.max_elevation_seek_bar);
+ mMaxElevationSeekBar.setProgress((int) mCardView.getMaxCardElevation());
+ mMaxElevationSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);
+
+ mAlphaSeekBar = (SeekBar) findViewById(R.id.alpha_seek_bar);
+ mAlphaSeekBar.setProgress((int) ViewCompat.getAlpha(mCardView) * 255);
+ mAlphaSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangedListener);
+
+ RadioGroup rb = (RadioGroup) findViewById(R.id.select_target_radio);
+ rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(RadioGroup group, int checkedId) {
+ mResizeCardView = checkedId == R.id.resize_card_view;
+ update();
+ }
+ });
+
update();
new Handler().postDelayed(new Runnable() {
@Override