0

I have searched literally everywhere on this but found no useful info or question on this.

How do I set onClickLister on search icon in SearchView.?

I changed the default search icon to my own drawable.

<androidx.appcompat.widget.SearchView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/action_search"
    android:layout_marginTop="20dp"
    app:queryHint="Search..."
    app:iconifiedByDefault="false"
    app:searchIcon="@drawable/back_icon2"
    android:theme="@style/WhiteCursorSearchView"
    android:layout_alignParentTop="true"
    />

app:searchIcon="@drawable/back_icon2"

Now I want to set onClick Listener to this new drawable which is in the searchView NOT the whole searchView.

Also, this is a search widget not a search dialog. I am not using this in any actionbar.

NOTE - I have done a comprehensive search and research on this but found nothing useful.

Ibramazin
  • 958
  • 10
  • 30

3 Answers3

0

Unfortunately, you can do this. I will suggest you to make an edittext and at the end put a button and set the icon. Now you can manually handle this. I think this will be perfect.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="fill_parent">
<EditText android:text="@+id/EditText01" 
    android:id="@+id/EditText01"
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:layout_width="fill_parent">
</EditText>

<ImageButton
 android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
  android:id="@+id/imageButton"
  android:layout_centerVertical="true"
  android:layout_centerHorizontal="true"
  android:src="@drawable/abc"/>
</LinearLayout>
Muntasir Aonik
  • 1,800
  • 1
  • 9
  • 22
0

there is a special listener for the purpose in SearchView called setOnSearchClickListener

Example

binding.searchView.setOnSearchClickListener {
}
0

For androidx SearchView:

<androidx.appcompat.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            app:iconifiedByDefault="false"
            app:searchIcon="@drawable/ic_back"
            app:layout_constrainedHeight="false" />

Here is how you can set click event of app:searchIcon

Kotlin:

searchView.findViewById<View>(androidx.appcompat.R.id.search_mag_icon).setOnClickListener {
                // Write your code here for the actions you need on click of searchIcon

            }

Java:

searchView.findViewById<>(androidx.appcompat.R.id.search_mag_icon).setOnClickListener {
                // Write your code here for the actions you need on click of searchIcon

            }

If you want to remove underline under the searchView, use below code.

searchView.findViewById<View>(androidx.appcompat.R.id.search_plate).setBackgroundColor(Color.TRANSPARENT)
Viraj Patel
  • 2,113
  • 16
  • 23