Search code examples
androidandroid-layoutandroid-fragmentssearchview

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


Solution

  • What is the clickable mean? trigger search action or just make the edit area focused? If it is the first, you can just make the icon clickable=false. and make the whole layout clickable and implement a event listener.

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

    The onClick method should be

    public void onClick(View v) {
       InputMethodManager im = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
        im.showSoftInput(editText, 0);
    }