Skip to content

Commit 34a016d

Browse files
authored
Change item background according to the theme (#3480)
* Change item background according to the theme * Change background colour of item on being selected * Change background colors using XML selectors
1 parent 81c7281 commit 34a016d

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/NearbyFilterSearchRecyclerViewAdapter.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public RecyclerViewHolder(View view) {
7070
@NonNull
7171
@Override
7272
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
73-
View itemView = inflater.inflate(callback.isDarkTheme()?R.layout.nearby_search_list_item_dark:R.layout.nearby_search_list_item, parent, false);
73+
View itemView = inflater.inflate(callback.isDarkTheme() ? R.layout.nearby_search_list_item_dark : R.layout.nearby_search_list_item, parent, false);
7474
return new RecyclerViewHolder(itemView);
7575
}
7676

@@ -79,17 +79,20 @@ public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
7979
Label label = displayedLabels.get(position);
8080
holder.placeTypeIcon.setImageResource(label.getIcon());
8181
holder.placeTypeLabel.setText(label.toString());
82+
holder.placeTypeLayout.setSelected(label.isSelected());
8283

83-
holder.placeTypeLayout.setBackgroundColor(label.isSelected() ? ContextCompat.getColor(context, R.color.divider_grey) : callback.isDarkTheme()?Color.BLACK:Color.WHITE);
8484
holder.placeTypeLayout.setOnClickListener(view -> {
8585
callback.setCheckboxUnknown();
86+
8687
if (label.isSelected()) {
8788
selectedLabels.remove(label);
8889
} else {
8990
selectedLabels.add(label);
9091
}
92+
9193
label.setSelected(!label.isSelected());
92-
holder.placeTypeLayout.setBackgroundColor(label.isSelected() ? ContextCompat.getColor(context, R.color.divider_grey) : Color.WHITE);
94+
holder.placeTypeLayout.setSelected(label.isSelected());
95+
9396
callback.filterByMarkerType(selectedLabels, 0, false, false);
9497
});
9598
}
@@ -165,7 +168,7 @@ public void setRecyclerViewAdapterAllSelected() {
165168
notifyDataSetChanged();
166169
}
167170

168-
public interface Callback{
171+
public interface Callback {
169172

170173
void setCheckboxUnknown();
171174

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:state_selected="true"
4+
android:drawable="@color/opak_middle_grey" />
5+
<item android:drawable="@color/black" />
6+
</selector>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:state_selected="true"
4+
android:drawable="@color/divider_grey" />
5+
<item android:drawable="@color/white" />
6+
</selector>
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
<?xml version="1.0" encoding="utf-8"?>
2+
23
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
34
android:id="@+id/search_list_item"
45
android:layout_width="match_parent"
56
android:layout_height="wrap_content"
67
android:orientation="horizontal"
7-
android:padding="@dimen/tiny_margin">
8+
android:padding="@dimen/tiny_margin"
9+
android:background="@drawable/linearlayout_color_selector">
810

911
<ImageView
1012
android:id="@+id/place_icon"
1113
android:layout_width="wrap_content"
12-
android:layout_height="wrap_content" />
14+
android:layout_height="wrap_content"/>
1315

1416
<TextView
1517
android:id="@+id/place_text"
1618
android:layout_width="match_parent"
17-
android:layout_height="wrap_content"></TextView>
19+
android:layout_height="wrap_content"/>
1820
</LinearLayout>

app/src/main/res/layout/nearby_search_list_item_dark.xml

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
android:layout_width="match_parent"
55
android:layout_height="wrap_content"
66
android:padding="@dimen/tiny_margin"
7-
android:orientation="horizontal">
7+
android:orientation="horizontal"
8+
android:background="@drawable/linearlayout_color_dark_selector">
89

910
<ImageView
1011
android:id="@+id/place_icon"
1112
android:layout_width="wrap_content"
12-
android:layout_height="wrap_content" />
13+
android:layout_height="wrap_content"/>
1314

1415
<TextView
1516
android:id="@+id/place_text"
1617
android:layout_width="match_parent"
1718
android:layout_height="wrap_content"
18-
android:textColor="@color/white"></TextView>
19+
android:textColor="@color/white"/>
1920
</LinearLayout>

0 commit comments

Comments
 (0)