Android - Make whole search bar clickable
The trick isn't so much to make the entire area clickable as much as it is to expand it and then hide the keyboard.
First, your layout in the XML is fine, leave it as is, in your Java, you want to have the following:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//Define your Searchview
SearchView searchView = (SearchView) findViewById(R.id.search_bar);
//Turn iconified to false:
searchView.setIconified(false);
//The above line will expand it to fit the area as well as throw up the keyboard
//To remove the keyboard, but make sure you keep the expanded version:
searchView.clearFocus();
}
What this accomplishes is it expands the keyboard to the entire length of the area, it focuses on it, which allows the entire area to be clickable, and then it removes the keyboard so that it looks to the user like they are able to click that field and bring up a keyboard.
Should accomplish what you are looking for.
-Sil
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"
add this to your xml
android:iconifiedByDefault="false"
it will keep open your searchview.and to clear it add
clearfocus
to your searchview object.
search_bar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
search_bar.onActionViewExpanded();
}
});