4

I want the android searchview in toolbar open without click on search icon. Here is the XML code

<item
    android:id="@+id/action_search"
    android:title="@string/search_title"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always"/>

And Java code in MainActivity.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    MenuItem menuItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
    searchView.setQueryHint("Type something...");
    int searchPlateId = searchView.getContext()
            .getResources()
            .getIdentifier("android:id/search_plate", null, null);
    View searchPlate = searchView.findViewById(searchPlateId);
    if (searchPlate != null) {
        searchPlate.setBackgroundColor(Color.DKGRAY);
        int searchTextId = searchPlate.getContext()
                .getResources()
                .getIdentifier("android:id/search_src_text", null, null);
        TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
        if (searchText != null) {
            searchText.setTextColor(Color.WHITE);
            searchText.setHintTextColor(Color.WHITE);
        }
    }
    return true;
}

This works and the output screen below ...

enter image description here

So, after I click Search Icon it output screen below ...

enter image description here

What I want is directly the second screen!

Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
  • 1
    in `onCreate` of second `Activity` do `search.performClick();` – Logic May 26 '15 at 11:05
  • @Logic that call automaticaly to `onclik` but.. Why you don't put directly a `Edittext` on toolbar and don't `search icon' ? You can add an icon on your edittext too – Aspicas May 26 '15 at 11:06
  • @Aspicas `EditText` needs to be designed to look like the default `Search` instead using the availbale one is easier and prefered – Logic May 26 '15 at 11:09
  • then... I agree with @Logic his solution can be works. – Aspicas May 26 '15 at 11:12
  • @Madan Sapkota I have a similar problem here: http://stackoverflow.com/questions/43702055/android-how-do-i-get-searchview-close-button-to-return-to-search-edittext?noredirect=1#comment74449117_43702055. Any thoughts or ideas on how to fix? – AJW Apr 30 '17 at 02:55

1 Answers1

16

Try to add this line to your code after initializing the SearchView:

searchView.setFocusable(true);

Update:

searchView.setIconified(false) worked 
Logic
  • 2,230
  • 2
  • 24
  • 41
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • 1
    Then this one `searchView.setIconified(false);` ? According to this SO question, it should be what you are looking for http://stackoverflow.com/questions/14235677/how-do-i-open-the-searchview-programmatically – Gabriella Angelova May 26 '15 at 11:11
  • `searchView.setIconified(false)` works! Thanks for your help. – Madan Sapkota May 26 '15 at 11:12
  • @Gabriella Angelova I have a similar problem here: http://stackoverflow.com/questions/43702055/android-how-do-i-get-searchview-close-button-to-return-to-search-edittext?noredirect=1#comment74449117_43702055. Any thoughts or ideas on how to fix? – AJW Apr 30 '17 at 02:54
  • Your code just HIDES the searchView icon, it doesn't make it go away. – iBEK Nov 27 '17 at 03:42
  • how to close the keyboard, but still open – Antonio Komang Yudistira Feb 13 '20 at 11:18