From 3971416d1556c6544a1e889d73251fb4959af300 Mon Sep 17 00:00:00 2001 From: David Luhmer Date: Thu, 11 Apr 2019 16:11:09 -0300 Subject: [PATCH] fix invisible color of cursor in search view / fix rotation issues of search view --- News-Android-App/src/main/AndroidManifest.xml | 2 +- .../NewsReaderListActivity.java | 47 +++++++++---- .../owncloudnewsreader/helper/ThemeUtils.java | 70 +++++++++++++++++++ .../src/main/res/drawable/cursor.xml | 6 ++ 4 files changed, 111 insertions(+), 14 deletions(-) create mode 100644 News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/ThemeUtils.java create mode 100644 News-Android-App/src/main/res/drawable/cursor.xml diff --git a/News-Android-App/src/main/AndroidManifest.xml b/News-Android-App/src/main/AndroidManifest.xml index 31c496c1..be01427e 100644 --- a/News-Android-App/src/main/AndroidManifest.xml +++ b/News-Android-App/src/main/AndroidManifest.xml @@ -32,7 +32,6 @@ tools:replace="android:icon, android:label, android:theme, android:name"> @@ -41,6 +40,7 @@ + searchPublishSubject; private static final int REQUEST_CODE_PERMISSION_DOWNLOAD_WEB_ARCHIVE = 1; @@ -159,9 +162,11 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements private static final String LIST_ADAPTER_TOTAL_COUNT = "LIST_ADAPTER_TOTAL_COUNT"; private static final String LIST_ADAPTER_PAGE_COUNT = "LIST_ADAPTER_PAGE_COUNT"; + @Inject @Named("sharedPreferencesFileName") String sharedPreferencesFileName; + @Override protected void onCreate(Bundle savedInstanceState) { ((NewsReaderApplication) getApplication()).getAppComponent().injectActivity(this); @@ -299,6 +304,10 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements outState.putInt(LIST_ADAPTER_PAGE_COUNT, adapter.getCachedPages()); } } + if(mSearchView != null) { + mSearchString = mSearchView.getQuery().toString(); + outState.putString(SEARCH_KEY, mSearchString); + } } private void restoreInstanceState(Bundle savedInstanceState) { @@ -320,6 +329,7 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements savedInstanceState.getLong(ID_FEED_STRING), false); } + mSearchString = savedInstanceState.getString(SEARCH_KEY, null); } @@ -696,27 +706,38 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements @Override public boolean onMenuItemActionCollapse(MenuItem item) { + onQueryTextChange(""); // Reset search clearSearchViewFocus(); return true; } }); - searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); - searchView.setIconifiedByDefault(false); - searchView.setOnQueryTextListener(this); - searchView.setOnQueryTextFocusChangeListener((v, hasFocus) -> { + mSearchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); + mSearchView.setIconifiedByDefault(false); + mSearchView.setOnQueryTextListener(this); + mSearchView.setOnQueryTextFocusChangeListener((v, hasFocus) -> { if(!hasFocus) { clearSearchViewFocus(); } }); - NewsReaderDetailFragment ndf = getNewsReaderDetailFragment(); - if(ndf != null) - ndf.updateMenuItemsState(); + ThemeUtils.colorSearchViewCursorColor(mSearchView, Color.WHITE); + + NewsReaderDetailFragment ndf = getNewsReaderDetailFragment(); + if(ndf != null) { + ndf.updateMenuItemsState(); + } updateButtonLayout(); - return true; + // focus the SearchView (if search view was active before orientation change) + if (mSearchString != null && !mSearchString.isEmpty()) { + searchItem.expandActionView(); + mSearchView.setQuery(mSearchString, true); + mSearchView.clearFocus(); + } + + return true; } public MenuItem getMenuItemDownloadMoreItems() { @@ -812,9 +833,9 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements return true; case R.id.menu_search: - searchView.setIconified(false); - searchView.setFocusable(true); - searchView.requestFocusFromTouch(); + mSearchView.setIconified(false); + mSearchView.setFocusable(true); + mSearchView.requestFocusFromTouch(); return true; case R.id.menu_download_web_archive: @@ -1072,6 +1093,6 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements } public void clearSearchViewFocus() { - searchView.clearFocus(); + mSearchView.clearFocus(); } } diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/ThemeUtils.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/ThemeUtils.java new file mode 100644 index 00000000..77a51132 --- /dev/null +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/ThemeUtils.java @@ -0,0 +1,70 @@ +package de.luhmer.owncloudnewsreader.helper; + +import android.graphics.PorterDuff; +import android.graphics.drawable.Drawable; +import android.util.Log; +import android.widget.SearchView; +import android.widget.TextView; + +import java.lang.reflect.Field; + +import androidx.annotation.ColorInt; +import androidx.core.content.ContextCompat; +import de.luhmer.owncloudnewsreader.R; + +public class ThemeUtils { + + private static final String TAG = ThemeUtils.class.getCanonicalName(); + + private ThemeUtils() {} + + /** + * Sets the color of the SearchView to {@code color} (cursor. + * @param searchView + */ + public static void colorSearchViewCursorColor(SearchView searchView, @ColorInt int color) { + try { + Field searchTextViewRef = SearchView.class.getDeclaredField("mSearchSrcTextView"); + searchTextViewRef.setAccessible(true); + Object searchAutoComplete = searchTextViewRef.get(searchView); + + Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes"); + mCursorDrawableRes.setAccessible(true); + mCursorDrawableRes.set(searchAutoComplete, R.drawable.cursor); + + + // Set color of handle + // https://stackoverflow.com/a/49555923 + + //get the pointer resource id + Field textSelectHandleRef = TextView.class.getDeclaredField("mTextSelectHandleRes"); + textSelectHandleRef.setAccessible(true); + int drawableResId = textSelectHandleRef.getInt(searchAutoComplete); + + //get the editor + Field editorRef = TextView.class.getDeclaredField("mEditor"); + editorRef.setAccessible(true); + Object editor = editorRef.get(searchAutoComplete); + + //tint drawable + Drawable drawable = ContextCompat.getDrawable(searchView.getContext(), drawableResId); + drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); + + //set the drawable + Field mSelectHandleCenter = editor.getClass().getDeclaredField("mSelectHandleCenter"); + mSelectHandleCenter.setAccessible(true); + mSelectHandleCenter.set(editor, drawable); + + Field mSelectHandleLeft = editor.getClass().getDeclaredField("mSelectHandleLeft"); + mSelectHandleLeft.setAccessible(true); + mSelectHandleLeft.set(editor, drawable); + + Field mSelectHandleRight = editor.getClass().getDeclaredField("mSelectHandleRight"); + mSelectHandleRight.setAccessible(true); + mSelectHandleRight.set(editor, drawable); + } catch (Exception e) { + Log.e(TAG, "Couldn't apply color to search view cursor", e); + } + } + +} diff --git a/News-Android-App/src/main/res/drawable/cursor.xml b/News-Android-App/src/main/res/drawable/cursor.xml new file mode 100644 index 00000000..04c211be --- /dev/null +++ b/News-Android-App/src/main/res/drawable/cursor.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file