0

I'm trying to add an icon at the end of my SearchView like this

enter image description here

Here's my actual code:

<RelativeLayout
    android:id="@+id/home_relativelayout_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/color_primary">
    <android.support.v7.widget.SearchView
        android:id="@+id/search_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColorHint="@color/grey_medium"
        android:layout_margin="@dimen/mpoint_divider_padding"
        android:gravity="center"
        android:layout_centerVertical="true"
        android:inputType="textNoSuggestions"
        android:background="@drawable/wo_search_background" />
</RelativeLayout>

Seems there are no property such as android:drawableRight, I even tried to add a child like an ImageView but nothing appears on screen

Is it possible to achieve what I want with a SearchView or should I switch to an EditText+ImageView ?

Thanks for your help

moueidat
  • 529
  • 1
  • 7
  • 21
  • You can custom `SearchView` extend `LinearLayout` , and use `EditText`+`ImageView` to show what you want. – Junior Jiang Dec 04 '18 at 02:26
  • I would suggest you take a look at my answer [ClearableEditext](https://stackoverflow.com/a/49494031/7462031), Where you can edit the existing code and achieve what you want. – FreakyAli Dec 04 '18 at 06:54

1 Answers1

0

You can do it something like this:

Add the ShapeDrawable something like this to your drawable folder:

Border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
 android:shape="rectangle" >
  <solid android:color="@android:color/white" /> //Background color here
  <stroke android:width="3dip" android:color="*Border color*"/>
</shape>

Then Add the SearchView as follows:

 <LinearLayout 
    android:layout_width="match_parent"
    android:background="@drawable/Border"
    android:layout_height="wrap_content" 
    android:Orientation="horizontal">
 <android.support.v7.widget.SearchView
    android:id="@+id/search_view"
    android:layout_width="0dp" 
    android:layout_weight="4"
    android:layout_height="wrap_content"
    android:textColorHint="@color/grey_medium"
    android:layout_margin="@dimen/mpoint_divider_padding"
    android:gravity="center"
    android:layout_centerVertical="true"
    android:inputType="textNoSuggestions"
    android:background="@drawable/wo_search_background" />
   <ImageView   android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    .../>
  </LinearLayout>

Using this you can handle the click event by adding it to the image.

Revert in case of queries and if it doesn't work.

FreakyAli
  • 13,349
  • 3
  • 23
  • 63