fix invisible color of cursor in search view / fix rotation issues of search view

This commit is contained in:
David Luhmer 2019-04-11 16:11:09 -03:00
parent 842b7be3a3
commit 3971416d15
4 changed files with 111 additions and 14 deletions

View file

@ -32,7 +32,6 @@
tools:replace="android:icon, android:label, android:theme, android:name">
<activity
android:name=".NewsReaderListActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
@ -41,6 +40,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- android:configChanges="keyboardHidden|orientation|screenSize" -->
<activity
android:name=".NewsDetailActivity"
android:configChanges="keyboardHidden|orientation|screenSize"

View file

@ -97,6 +97,7 @@ import de.luhmer.owncloudnewsreader.database.model.RssItem;
import de.luhmer.owncloudnewsreader.events.podcast.FeedPanelSlideEvent;
import de.luhmer.owncloudnewsreader.helper.DatabaseUtils;
import de.luhmer.owncloudnewsreader.helper.ThemeChooser;
import de.luhmer.owncloudnewsreader.helper.ThemeUtils;
import de.luhmer.owncloudnewsreader.reader.nextcloud.RssItemObservable;
import de.luhmer.owncloudnewsreader.services.DownloadImagesService;
import de.luhmer.owncloudnewsreader.services.DownloadWebPageService;
@ -148,7 +149,9 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
@VisibleForTesting @Nullable @BindView(R.id.drawer_layout) public DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private SearchView searchView;
private SearchView mSearchView;
private String mSearchString;
private static final String SEARCH_KEY = "SEARCH_KEY";
private PublishSubject<String> 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();
}
}

View file

@ -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);
}
}
}

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffff" />
<size android:width="2dp" />
</shape>