Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Make whole search bar clickable

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

like image 888
Saran Avatar asked May 26 '15 10:05

Saran


People also ask

How to make search View clickable in Android?

By default the SearchView is 'iconified', which is displayed as a magnifying glass icon and only if the user clicks on the icon, then the edit field expands. To enable the user to click anywhere on the SearchView and expand the input field.

How do I expand SearchView?

To make the SearchView expanded by default, call setIconifiedByDefault(false) on it when you initialise it (e.g. in onCreateOptionsMenu(..) or onPrepareOptionsMenu(..) ). I've found in most cases this will give it focus automatically, but if not simply call requestFocus() on it too.


2 Answers

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" 
like image 68
Zeeshan Ghazanfar Avatar answered Oct 04 '22 03:10

Zeeshan Ghazanfar


search_bar.setOnClickListener(new OnClickListener() {     @Override     public void onClick(View v) {         search_bar.onActionViewExpanded();     } }); 
like image 32
Lokesh Gupta Avatar answered Oct 04 '22 04:10

Lokesh Gupta