Skip to content

Commit 2a2460f

Browse files
committed
More prominent highlight than ripple
The ripple effect only takes a very short time and has low contrast. Now we just set the background color of the list item and keep it that way. This should be a lot easier to see.
1 parent e2a168c commit 2a2460f

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

lib/src/main/java/com/bytehamster/lib/preferencesearch/SearchPreferenceResult.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package com.bytehamster.lib.preferencesearch;
22

3-
import android.annotation.TargetApi;
3+
import android.content.Context;
44
import android.content.res.Resources;
55
import android.content.res.TypedArray;
66
import android.graphics.PorterDuff;
77
import android.graphics.drawable.Drawable;
8-
import android.graphics.drawable.RippleDrawable;
9-
import android.os.Build;
108
import android.os.Handler;
119
import android.util.Log;
1210
import android.util.TypedValue;
@@ -18,7 +16,6 @@
1816
import androidx.preference.PreferenceGroup;
1917
import androidx.recyclerview.widget.RecyclerView;
2018

21-
2219
public class SearchPreferenceResult {
2320
private final String key;
2421
private final int file;
@@ -79,11 +76,11 @@ private void doHighlight(final PreferenceFragmentCompat prefsFragment) {
7976
recyclerView.postDelayed(() -> {
8077
RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(position);
8178
if (holder != null) {
82-
Drawable background = holder.itemView.getBackground();
83-
if (Build.VERSION.SDK_INT >= 21 && background instanceof RippleDrawable) {
84-
forceRippleAnimation((RippleDrawable) background);
85-
return;
86-
}
79+
Drawable oldBackground = holder.itemView.getBackground();
80+
int color = getColorFromAttr(prefsFragment.getContext(), android.R.attr.textColorPrimary);
81+
holder.itemView.setBackgroundColor(color & 0xffffff | 0x33000000);
82+
new Handler().postDelayed(() -> holder.itemView.setBackgroundDrawable(oldBackground), 1000);
83+
return;
8784
}
8885
highlightFallback(prefsFragment, prefResult);
8986
}, 200);
@@ -94,20 +91,13 @@ private void doHighlight(final PreferenceFragmentCompat prefsFragment) {
9491
}
9592

9693
/**
97-
* Alternative (old) highlight method if ripple does not work
94+
* Alternative highlight method if accessing the view did not work
9895
*/
9996
private void highlightFallback(PreferenceFragmentCompat prefsFragment, final Preference prefResult) {
100-
TypedValue typedValue = new TypedValue();
101-
Resources.Theme theme = prefsFragment.getActivity().getTheme();
102-
theme.resolveAttribute(android.R.attr.textColorPrimary, typedValue, true);
103-
TypedArray arr = prefsFragment.getActivity().obtainStyledAttributes(typedValue.data, new int[]{
104-
android.R.attr.textColorPrimary});
105-
int color = arr.getColor(0, 0xff3F51B5);
106-
arr.recycle();
107-
10897
final Drawable oldIcon = prefResult.getIcon();
10998
final boolean oldSpaceReserved = prefResult.isIconSpaceReserved();
11099
Drawable arrow = AppCompatResources.getDrawable(prefsFragment.getContext(), R.drawable.searchpreference_ic_arrow_right);
100+
int color = getColorFromAttr(prefsFragment.getContext(), android.R.attr.textColorPrimary);
111101
arrow.setColorFilter(color, PorterDuff.Mode.SRC_IN);
112102
prefResult.setIcon(arrow);
113103
prefsFragment.scrollToPreference(prefResult);
@@ -117,12 +107,15 @@ private void highlightFallback(PreferenceFragmentCompat prefsFragment, final Pre
117107
}, 1000);
118108
}
119109

120-
@TargetApi(21)
121-
protected void forceRippleAnimation(RippleDrawable background) {
122-
final RippleDrawable rippleDrawable = background;
123-
Handler handler = new Handler();
124-
rippleDrawable.setState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled});
125-
handler.postDelayed(() -> rippleDrawable.setState(new int[]{}), 1000);
110+
private int getColorFromAttr(Context context, int attr) {
111+
TypedValue typedValue = new TypedValue();
112+
Resources.Theme theme = context.getTheme();
113+
theme.resolveAttribute(attr, typedValue, true);
114+
TypedArray arr = context.obtainStyledAttributes(typedValue.data, new int[]{
115+
android.R.attr.textColorPrimary});
116+
int color = arr.getColor(0, 0xff3F51B5);
117+
arr.recycle();
118+
return color;
126119
}
127120

128121
/**

0 commit comments

Comments
 (0)