Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style custom search suggestion list in Android

I implemented custom suggestions by content provider based on this tutorial; it works, however I have a problem styling the suggestion list. The suggestion list I currently have has black background and white text. I want to change it to white background and black text. I did some search online and tried something like this in styles.xml file:

<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
    <item name="suggestionRowLayout">@layout/list_item_search_suggestion</item>
</style>
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="searchViewStyle">@style/MySearchViewStyle</item>
</style>

My list_item_search_suggestion looks like this:

    <?xml version="1.0" encoding="utf-8"?>
<TextView
    android:id="@android:id/text1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:gravity="center_vertical"
    android:minHeight="56dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="@android:color/black"
    tools:text="Blah blah blah"/>

However, when I run the app and search something, it crashes before the suggestion list shows up. I get a NullPointerException from newView method in SuggestionsAdapter. This SuggestionAdapter is from SDK, I'm not implementing my own adapter. I just implement the content provider and set that in searchable.xml. Also the stack track is something like this:

06-15 21:11:09.253    4063-4063/com.tophatter E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.tophatter, PID: 4063
java.lang.NullPointerException
        at android.support.v7.widget.SuggestionsAdapter.newView(SuggestionsAdapter.java:249)
        at android.support.v7.widget.SuggestionsAdapter.getView(SuggestionsAdapter.java:453)
        at android.widget.AbsListView.obtainView(AbsListView.java:2255)
        at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1585)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
        at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1167)
        at android.widget.ListPopupWindow.show(ListPopupWindow.java:554)
        at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1096)
        at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:971)
        at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:953)
        at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
like image 590
Fei Qu Avatar asked Oct 31 '22 01:10

Fei Qu


1 Answers

I figured this out. Basically SuggestionsAdapter is referring some widgets I was not providing in my custom suggestion row layout. So I copied default custom suggestion row layout from source code and changed background and text color as my custom layout. It's working now.

like image 85
Fei Qu Avatar answered Nov 15 '22 06:11

Fei Qu