Android - Prevent text truncation in SearchView suggestions?

℡╲_俬逩灬. 提交于 2019-11-28 11:21:44

It seems like you want a custom layout for your search view results. I will try to outline some clear steps below:

To make the search view to work we need to have a searchable.xml in the res/xml folder and a content provider at all times.

searchable.xml example:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:searchSuggestAuthority="com.example.searchProvider" ></searchable>

The label and searchSuggestAuthority are required. The searchSuggestAuthority should point to the location of the content provider

Add the intent-filter meta-data to the activity in the manifest. Example:

  <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>

In the activity/fragment, get your searchView object and pass it the custom adapter for displaying the custom layout.

searchView.setSearchableInfo(manager.getSearchableInfo(getActivity().getComponentName()));
        final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
               //DO whatever you want here on text submit in the search View

                return true;
            }
 @Override
            public boolean onQueryTextChange(String textChange) {


                searchView.setSuggestionsAdapter(new ExampleAdapter(context,yourData));

            }
        };

Make sure the custom adapter extends the cursorAdapter.

public class ExampleAdapter extends CursorAdapter {

    private Cursor cursor;

    private TextView text;

    public ExampleAdapter(Context context, Cursor cursor) {

        super(context, cursor);

        this.cursor= cursor;

    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        text.setText((cursor.getString(cursor.getColumnIndex(YOUR_STRING_KEY));));

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = inflater.inflate(R.layout.item, parent, false);

        text = (TextView) view.findViewById(R.id.text);

        return view;

    }

}

In the above code R.layout.item refers to your custom xml file that you can use to inflate in the searchview results.

Make sure you are using a content provider though. The searchView doesn't work without one. It doesn't matter even if you are only caching the data on a temporary basis.

Hope this helps!

user2511882's answer is perfect, but that constructor is deprecated.

Deprecated :
public ExampleAdapter(Context context, Cursor cursor) {
    super(context, cursor);
    this.cursor= cursor;
}

you can use the below one instead of that.

public ExampleAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
}

and then call it like this.

searchView.setSuggestionsAdapter(new ExampleAdapter(context,YOUR_DATA,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER));

https://developer.android.com/reference/android/widget/CursorAdapter.html

I think you will need to create a CustomAdapter that access your own layout to do this. It is surprising this hasn't been made easier to modify. SuggestionsAdapter isn't public so you can't extend it. It's not pretty but the easiest solution will be to copy SuggestionsAdapter.java into your own project and point it to a layout in your own res/layouts. I would again copy the version from the Android source code and modify it to your needs.

The Android 5 version is even worse for modifying so this is the KitKat version: http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/android/widget/SuggestionsAdapter.java/?v=source

Change this line to point to your layout file:

super(context,
        com.android.internal.R.layout.search_dropdown_item_icons_2line, // make it you layout xml (see below)
        null,   // no initial cursor
        true);  // auto-requery

http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/frameworks/base/core/res/res/layout/search_dropdown_item_icons_2line.xml/?v=source

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingStart="@dimen/dropdownitem_text_padding_left"
    android:paddingEnd="4dip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" > <!-- changed -->

Also in the layout file:

<TextView android:id="@android:id/text2"
    style="?android:attr/dropDownItemStyle"
    android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle"
    <!-- android:singleLine="true" --> <!-- remove this line -->
    android:layout_width="match_parent"
    android:layout_height="wrap_content" <!-- changed -->

user2511882's answer is probably better than this quick hack.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!