11package com .bytehamster .lib .preferencesearch ;
22
3- import android .annotation . TargetApi ;
3+ import android .content . Context ;
44import android .content .res .Resources ;
55import android .content .res .TypedArray ;
66import android .graphics .PorterDuff ;
77import android .graphics .drawable .Drawable ;
8- import android .graphics .drawable .RippleDrawable ;
9- import android .os .Build ;
108import android .os .Handler ;
119import android .util .Log ;
1210import android .util .TypedValue ;
1816import androidx .preference .PreferenceGroup ;
1917import androidx .recyclerview .widget .RecyclerView ;
2018
21-
2219public 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