Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open android search without click?

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!

like image 495
Madan Sapkota Avatar asked Dec 06 '22 21:12

Madan Sapkota


1 Answers

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

searchView.setFocusable(true);

Update:

searchView.setIconified(false) worked 
like image 196
Gabriella Angelova Avatar answered Dec 27 '22 18:12

Gabriella Angelova