Android - Make whole search bar clickable

Saran picture Saran · May 26, 2015 · Viewed 21.2k times · Source

I am using a search-view element in my fragment to implement search feature.

<SearchView
    android:id="@+id/search_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="7dp"
    android:layout_marginLeft="7dp"
    android:layout_marginRight="7dp"
    android:layout_marginBottom="7dp"
    android:background="@color/white" />

enter image description here

The problem is only the search icon is clickable other area in the search bar is not clickable, when i click the icon only i can able to search.

enter image description here

Can you please help me to make the whole search area clickable.

enter image description here

Answer

Zeeshan Ghazanfar picture Zeeshan Ghazanfar · Jun 11, 2016

This can be simply done by setting Iconified to false on OnClick of SearchView.

searchBar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        searchBar.setIconified(false);
    }
});

Reference: Eric Lui's answer

Hopefully it will help.

UPDATE:

You can also use it directly in your XML

app:iconifiedByDefault="false"