Skip to content

Commit 4cbe3b1

Browse files
ymarianikim24
authored andcommitted
Support checkable in card view, add states demo.
Also added a demo showing the different states PiperOrigin-RevId: 227719145
1 parent db4c951 commit 4cbe3b1

11 files changed

Lines changed: 407 additions & 32 deletions

File tree

catalog/java/io/material/catalog/card/CardFragment.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import io.material.catalog.feature.Demo;
2929
import io.material.catalog.feature.DemoLandingFragment;
3030
import io.material.catalog.feature.FeatureDemo;
31-
import java.util.Collections;
31+
import java.util.Arrays;
3232
import java.util.List;
3333

3434
/** A landing fragment that links to card demos for the Catalog app. */
@@ -56,13 +56,21 @@ public Fragment createFragment() {
5656

5757
@Override
5858
public List<Demo> getAdditionalDemos() {
59-
return Collections.singletonList(new Demo(R.string.cat_card_draggable_card) {
60-
@Nullable
61-
@Override
62-
public Fragment createFragment() {
63-
return new DraggableCardFragment();
64-
}
65-
});
59+
return Arrays.asList(
60+
new Demo(R.string.cat_card_draggable_card) {
61+
@Nullable
62+
@Override
63+
public Fragment createFragment() {
64+
return new DraggableCardFragment();
65+
}
66+
},
67+
new Demo(R.string.cat_card_states) {
68+
@Nullable
69+
@Override
70+
public Fragment createFragment() {
71+
return new CardStatesFragment();
72+
}
73+
});
6674
}
6775

6876
/** The Dagger module for {@link CardFragment} dependencies. */
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.material.catalog.card;
2+
3+
import io.material.catalog.R;
4+
5+
/*
6+
* Copyright 2018 The Android Open Source Project
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
import android.os.Bundle;
22+
import androidx.annotation.Nullable;
23+
import com.google.android.material.card.MaterialCardView;
24+
import android.view.LayoutInflater;
25+
import android.view.View;
26+
import android.view.ViewGroup;
27+
import android.widget.RadioGroup;
28+
import io.material.catalog.feature.DemoFragment;
29+
30+
/** A fragment showing {@link MaterialCardView} states */
31+
public class CardStatesFragment extends DemoFragment {
32+
33+
@Override
34+
public int getDemoTitleResId() {
35+
return R.string.cat_card_states;
36+
}
37+
38+
@Override
39+
public View onCreateDemoView(
40+
LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
41+
View view =
42+
layoutInflater.inflate(
43+
R.layout.cat_card_states_fragment, viewGroup, false /* attachToRoot */);
44+
RadioGroup radioGroup = view.findViewById(R.id.cat_card_radio_group);
45+
final MaterialCardView card = view.findViewById(R.id.card);
46+
final MaterialCardView checkableCard = view.findViewById(R.id.checkable_card);
47+
48+
radioGroup.setOnCheckedChangeListener(
49+
new RadioGroup.OnCheckedChangeListener() {
50+
@Override
51+
public void onCheckedChanged(RadioGroup group, int checkedId) {
52+
card.setHovered(checkedId == R.id.hovered);
53+
card.setPressed(checkedId == R.id.pressed);
54+
checkableCard.setHovered(checkedId == R.id.hovered);
55+
checkableCard.setPressed(checkedId == R.id.pressed);
56+
}
57+
});
58+
59+
return view;
60+
}
61+
}

catalog/java/io/material/catalog/card/DraggableCardFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public View onCreateDemoView(
4747
LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
4848
View view =
4949
layoutInflater.inflate(
50-
R.layout.cat_card_draggable_demo, viewGroup, false /* attachToRoot */);
50+
R.layout.cat_card_draggable_fragment, viewGroup, false /* attachToRoot */);
5151
DraggableCoordinatorLayout container = (DraggableCoordinatorLayout) view;
5252
final MaterialCardView card = view.findViewById(R.id.draggable_card);
5353
container.addDraggableChild(card);

catalog/java/io/material/catalog/card/res/layout/cat_card_draggable_demo.xml renamed to catalog/java/io/material/catalog/card/res/layout/cat_card_draggable_fragment.xml

File renamed without changes.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2018 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<LinearLayout
19+
xmlns:android="http://schemas.android.com/apk/res/android"
20+
xmlns:app="http://schemas.android.com/apk/res-auto"
21+
android:layout_width="match_parent"
22+
android:layout_height="match_parent"
23+
android:padding="16dp"
24+
android:orientation="vertical">
25+
26+
<com.google.android.material.card.MaterialCardView
27+
android:id="@+id/card"
28+
android:layout_width="match_parent"
29+
android:layout_height="wrap_content"
30+
android:layout_margin="16dp"
31+
android:clickable="true"
32+
android:focusable="true"
33+
android:minHeight="@dimen/cat_card_demo_min_height"
34+
app:contentPadding="16dp">
35+
36+
<TextView
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:layout_gravity="center"
40+
android:text="@string/cat_card_states_card_content"
41+
android:textAppearance="?attr/textAppearanceBody1"/>
42+
</com.google.android.material.card.MaterialCardView>
43+
44+
<com.google.android.material.card.MaterialCardView
45+
android:id="@+id/checkable_card"
46+
style="@style/Widget.MaterialComponents.CardView"
47+
android:layout_width="match_parent"
48+
android:layout_height="wrap_content"
49+
android:layout_margin="16dp"
50+
android:clickable="true"
51+
android:focusable="true"
52+
android:checkable="true"
53+
android:minHeight="@dimen/cat_card_demo_min_height"
54+
app:contentPadding="16dp">
55+
56+
<TextView
57+
android:layout_width="wrap_content"
58+
android:layout_height="wrap_content"
59+
android:layout_gravity="center"
60+
android:text="@string/cat_card_checkable_card"
61+
android:textAppearance="?attr/textAppearanceBody1"/>
62+
63+
</com.google.android.material.card.MaterialCardView>
64+
65+
<TextView
66+
android:layout_width="wrap_content"
67+
android:layout_height="wrap_content"
68+
android:layout_marginTop="16dp"
69+
android:layout_marginBottom="8dp"
70+
android:text="@string/cat_card_select"
71+
android:textAppearance="?attr/textAppearanceBody1"/>
72+
73+
<RadioGroup
74+
android:id="@+id/cat_card_radio_group"
75+
android:layout_width="match_parent"
76+
android:layout_height="wrap_content"
77+
android:layout_marginBottom="8dp"
78+
android:layout_gravity="center"
79+
android:orientation="horizontal">
80+
81+
<RadioButton
82+
android:id="@+id/pressed"
83+
android:layout_width="0dp"
84+
android:layout_height="wrap_content"
85+
android:layout_weight="1"
86+
android:text="@string/cat_card_state_pressed"/>
87+
<RadioButton
88+
android:id="@+id/hovered"
89+
android:layout_width="0dp"
90+
android:layout_height="wrap_content"
91+
android:layout_weight="1"
92+
android:text="@string/cat_card_state_hovered"/>
93+
<RadioButton
94+
android:id="@+id/none"
95+
android:layout_width="0dp"
96+
android:layout_height="wrap_content"
97+
android:layout_weight="1"
98+
android:text="@string/cat_card_state_none"/>
99+
100+
</RadioGroup>
101+
102+
</LinearLayout>

catalog/java/io/material/catalog/card/res/values/strings.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919

2020
<string name="cat_card_title">Cards</string>
2121
<string name="cat_card_draggable_card">Draggable Card</string>
22+
<string name="cat_card_states">Card States</string>
2223
<string name="cat_card_draggable_card_content">This is a draggable Card.</string>
24+
<string name="cat_card_states_card_content">This demo shows the different states on a card.</string>
25+
<string name="cat_card_state_hovered">Hovered</string>
26+
<string name="cat_card_state_pressed">Pressed</string>
27+
<string name="cat_card_state_none">None</string>
28+
<string name="cat_card_checkable_card">This is a checkable Card</string>
2329
<string name="cat_card_m1_section_title">Material 1 CardView</string>
2430
<string name="cat_card_description">
2531
Cards represent entry points into deeper levels of detail or navigation, such as an album in
@@ -44,4 +50,5 @@
4450
use MaterialCardView in your layout, or apply a style tag to a CardView. We recommend using
4551
the MaterialCardView class for the latest Material designs and maximum flexibility in your UI.
4652
</string>
53+
<string name="cat_card_select">Select a state</string>
4754
</resources>

0 commit comments

Comments
 (0)