I have implemented a search interface with recent searches suggestions as explained in the documentation, and for some reason the history icon comes from the wrong theme (dark instead of light.
My current task is to change this icon. I believe my theming is correct so I am trying to override the SearchRecentSuggestionsProvider
but without any success. I took inspiration from 2 posts (there and there) I have tried the following things:
Override query()
and specify another projection
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Uri iconUri = Uri.parse("android.resource://" + getContext().getPackageName() + "/drawable/icon_search_history");
projection = new String[] {
"_id",
"display1 AS " + SearchManager.SUGGEST_COLUMN_TEXT_1,
"query AS " + SearchManager.SUGGEST_COLUMN_QUERY,
"'" + iconUri + "'" + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1
};
return super.query(uri, projection, selection, selectionArgs, sortOrder);
}
I also tried overriding setupSuggestions()
@Override
protected void setupSuggestions(String authority, int mode) {
super.setupSuggestions(authority, mode);
Uri iconUri = Uri.parse("android.resource://" + RFDApplication.getApplicationPackageName() + "/drawable/icon_search_history");
Field f = null;
try {
f = getClass().getDeclaredField("mSuggestionProjection");
f.setAccessible(true);
String[] projection = new String [] {
"0 AS " + SearchManager.SUGGEST_COLUMN_FORMAT,
"'" + iconUri + "'" + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1,
"display1 AS " + SearchManager.SUGGEST_COLUMN_TEXT_1,
"query AS " + SearchManager.SUGGEST_COLUMN_QUERY,
"_id"};
f.set(this,projection);
} catch (Exception e) {
e.printStackTrace();
}
}
I never imagined I would have such a hard time changing an icon :) Any help or suggestion would be much appreciated...
EDIT
I found a half satisfactory solution: replacing the whole cursor in query()
. I would still prefer to avoid going iterating the results twice (although I know the overhead might be negligible in this case). So the question remains open
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
// File to use
Cursor parent = super.query(uri, projection, selection, selectionArgs, sortOrder);
Uri iconUri = Uri.parse("android.resource://" + getContext().getPackageName() + "/drawable/icon_search_history");
MatrixCursor cursor = new MatrixCursor(parent.getColumnNames());
parent.moveToFirst();
while (parent.moveToNext()){
cursor.addRow(new Object[]{
parent.getInt(parent.getColumnIndex("suggest_format")),
iconUri,
parent.getString(parent.getColumnIndex("suggest_text_1")),
parent.getString(parent.getColumnIndex("suggest_intent_query")),
parent.getInt(parent.getColumnIndex("_id"))
});
}
return cursor;
}