Skip to content

Commit 14a6267

Browse files
imhappidrchen
authored andcommitted
[Lists] Add selector for optional leading icon
PiperOrigin-RevId: 800665410
1 parent d241d48 commit 14a6267

File tree

8 files changed

+102
-5
lines changed

8 files changed

+102
-5
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!--
2+
Copyright 2025 The Android Open Source Project
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
18+
<item android:state_selected="true"
19+
android:drawable="@drawable/ic_star_icon_fill_24px"/>
20+
<item android:drawable="@drawable/ic_star_icon_24px"/>
21+
</selector>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!--
2+
Copyright 2025 The Android Open Source Project
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="960" android:viewportHeight="960" android:tint="?attr/colorControlNormal">
17+
<path android:fillColor="@android:color/white" android:pathData="M320,720L480,598L640,720L580,522L740,408L544,408L480,200L416,408L220,408L380,522L320,720ZM480,880Q397,880 324,848.5Q251,817 197,763Q143,709 111.5,636Q80,563 80,480Q80,397 111.5,324Q143,251 197,197Q251,143 324,111.5Q397,80 480,80Q563,80 636,111.5Q709,143 763,197Q817,251 848.5,324Q880,397 880,480Q880,563 848.5,636Q817,709 763,763Q709,817 636,848.5Q563,880 480,880Z"/>
18+
</vector>

catalog/java/io/material/catalog/listitem/ListsMainDemoFragment.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.view.LayoutInflater;
2929
import android.view.View;
3030
import android.view.ViewGroup;
31+
import android.widget.ImageView;
3132
import android.widget.TextView;
3233
import android.widget.Toast;
3334
import androidx.annotation.NonNull;
@@ -127,11 +128,12 @@ public static class CustomItemViewHolder extends ListItemViewHolder {
127128
private final TextView textView;
128129
private final MaterialCardView cardView;
129130

130-
131+
private final ImageView leadingIcon;
131132
public CustomItemViewHolder(@NonNull View itemView) {
132133
super(itemView);
133134
textView = itemView.findViewById(R.id.cat_list_item_text);
134135
cardView = itemView.findViewById(R.id.cat_list_item_card_view);
136+
leadingIcon = itemView.findViewById(R.id.cat_list_item_start_icon);
135137
}
136138

137139
public void bind(@NonNull CustomListItemData data) {
@@ -143,6 +145,7 @@ public void bind(@NonNull CustomListItemData data) {
143145
Toast.makeText(v.getContext(), R.string.mtrl_list_item_clicked, Toast.LENGTH_SHORT)
144146
.show();
145147
cardView.toggle();
148+
leadingIcon.setSelected(cardView.isChecked());
146149
data.checked = !data.checked;
147150
});
148151
}

catalog/java/io/material/catalog/listitem/res/layout/cat_list_item_segmented_viewholder.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
-->
1717
<com.google.android.material.listitem.ListItemLayout xmlns:android="http://schemas.android.com/apk/res/android"
1818
xmlns:tools="http://schemas.android.com/tools"
19+
xmlns:app="http://schemas.android.com/apk/res-auto"
1920
android:layout_width="match_parent"
2021
android:layout_height="wrap_content"
2122
style="?attr/listItemLayoutSegmentedStyle"
@@ -43,7 +44,8 @@
4344
android:layout_marginBottom="16dp"
4445
android:layout_marginStart="16dp"
4546
android:layout_marginEnd="16dp"
46-
android:src="@drawable/ic_star_icon_24px"
47+
style="@style/Widget.Material3.ImageView.ListItem.Leading"
48+
app:srcCompat="@drawable/ic_star_icon_checkable_24px"
4749
android:contentDescription="@string/cat_list_item_icon_content_description" />
4850

4951
<TextView

catalog/java/io/material/catalog/listitem/res/layout/cat_list_item_viewholder.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
-->
17-
<com.google.android.material.listitem.ListItemLayout
18-
xmlns:android="http://schemas.android.com/apk/res/android"
17+
<com.google.android.material.listitem.ListItemLayout xmlns:android="http://schemas.android.com/apk/res/android"
1918
xmlns:tools="http://schemas.android.com/tools"
2019
android:layout_width="match_parent"
2120
android:layout_height="wrap_content"
21+
xmlns:app="http://schemas.android.com/apk/res-auto"
2222
android:paddingHorizontal="8dp">
2323
<com.google.android.material.card.MaterialCardView
2424
android:id="@+id/cat_list_item_card_view"
@@ -43,7 +43,8 @@
4343
android:layout_marginBottom="16dp"
4444
android:layout_marginStart="16dp"
4545
android:layout_marginEnd="16dp"
46-
android:src="@drawable/ic_star_icon_24px"
46+
style="@style/Widget.Material3.ImageView.ListItem.Leading"
47+
app:srcCompat="@drawable/ic_star_icon_checkable_24px"
4748
android:contentDescription="@string/cat_list_item_icon_content_description" />
4849

4950
<TextView

lib/java/com/google/android/material/listitem/res-public/values/public.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
<resources>
1818
<public name="listItemLayoutStyle" type="attr"/>
1919
<public name="listItemLayoutSegmentedStyle" type="attr"/>
20+
<public name="Widget.Material3.ListItemLayout" type="style"/>
21+
<public name="ThemeOverlay.Material3.ListItemLayout" type="style"/>
22+
<public name="ThemeOverlay.Material3.ListItemLayout.Segmented" type="style"/>
23+
<public name="Widget.Material3.CardView.ListItemLayout" type="style"/>
24+
<public name="Widget.Material3.CardView.ListItemLayout.Segmented" type="style"/>
25+
<public name="ShapeAppearance.Material3.ListItem.First" type="style"/>
26+
<public name="ShapeAppearance.Material3.ListItem.Middle" type="style"/>
27+
<public name="ShapeAppearance.Material3.ListItem.Last" type="style"/>
28+
<public name="ShapeAppearance.Material3.ListItem.Single" type="style"/>
29+
<public name="ShapeAppearance.Material3.ListItem.Checked" type="style"/>
30+
<public name="Widget.Material3.ImageView.ListItem.Leading" type="style"/>
2031

2132
<public name="listItemShapeAppearanceSingle" type="attr"/>
2233
<public name="listItemShapeAppearanceFirst" type="attr"/>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2025 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+
https://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+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
18+
<!-- Hovered and selected state -->
19+
<item android:color="?attr/colorOnSecondaryContainer" android:state_hovered="true" android:state_selected="true"/>
20+
<!-- Focused and selected state -->
21+
<item android:color="?attr/colorOnSecondaryContainer" android:state_focused="true" android:state_selected="true"/>
22+
<!-- Pressed and selected state -->
23+
<item android:color="?attr/colorOnSecondaryContainer" android:state_pressed="true" android:state_selected="true"/>
24+
<!-- Pressed state -->
25+
<item android:color="?attr/colorOnSurfaceVariant" android:state_pressed="true"/>
26+
<!-- Focused state -->
27+
<item android:color="?attr/colorOnSurfaceVariant" android:state_focused="true"/>
28+
<!-- Disabled state -->
29+
<item android:color="?attr/colorOnSurface" android:state_enabled="false"/>
30+
<!-- Selected state -->
31+
<item android:color="?attr/colorOnSecondaryContainer" android:state_selected="true"/>
32+
<!-- Hovered state -->
33+
<item android:color="?attr/colorOnSurfaceVariant" android:state_hovered="true"/>
34+
<!-- Default, Enabled state -->
35+
<item android:color="?attr/colorOnSurfaceVariant"/>
36+
</selector>

lib/java/com/google/android/material/listitem/res/values/styles.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,9 @@
8080
<item name="cornerSize">?attr/shapeCornerSizeLarge</item>
8181
</style>
8282

83+
<!-- Styles for List item content. -->
84+
<style name="Widget.Material3.ImageView.ListItem.Leading" parent="">
85+
<item name="tint">@color/m3_list_item_leading_icon_color_selector</item>
86+
</style>
87+
8388
</resources>

0 commit comments

Comments
 (0)