Search code examples
androidsearchview

Android searchView, Show first part of long string instead of last


If I set the SearchView query to a string that is longer than the box, how do I make it such that it is the first part of the string that is visible instead of the last part? (I am using it as a navigation/search bar for a WebView.)

I do

mySearchView.setQuery("Start of long string.... at the end.", false);

And want it to show

Start of long

instead of

at the end

It's always showing me the end. I, essentially, want it scrolled back to the start of the text.

Edit: I do want the complete text to be there, just scrolled to the start.


Solution

  • If I remember well you need to get a hold of the EditText in the SearchView and then set the ellipsize on it.

    int id = mySearchView.getContext()
        .getResources()
        .getIdentifier("android:id/search_src_text", null, null);
    EditText editText = (EditText) mySearchView.findViewById(id);
    editText.setEllipsize(TextUtils.TruncateAt.END);
    

    If you don't want to ellipsize you can use the same method and set the position in the EditText.

    int id = mySearchView.getContext()
        .getResources()
        .getIdentifier("android:id/search_src_text", null, null);
    EditText editText = (EditText) mySearchView.findViewById(id);
    editText.setSelection(0);