AlinaBM
AlinaBM

Reputation: 243

Searchview widget : move search icon to right

I have this searchView and i want to move the search icon to the right(if possible) enter image description here

my xml code:

 <android.widget.SearchView
    android:id="@+id/searchBox"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="7dp"
    android:layout_below="@+id/header"
    android:iconifiedByDefault="false"
    android:background="@drawable/searchrectangle"
    android:queryHint="@string/search_message">

</android.widget.SearchView>

Upvotes: 3

Views: 4624

Answers (2)

Irfaan Parrahoo
Irfaan Parrahoo

Reputation: 21

I hope this work. It works for me.

android:layoutDirection="rtl"

Upvotes: 2

pfulop
pfulop

Reputation: 1009

If I am correct this is source for searchview https://android.googlesource.com/platform/frameworks/support.git/+/jb-mr2-release/v7/appcompat/res/layout/abc_search_view.xml .

From what it looks like, it uses linear layout and that mentioned icon is in front of text. That means there is not a nice way to put it on right side.

There are basically 3 options left, or at least which came to my mind.

  1. use custom made layout with drawable and edittext, I think some of the effects can be done using CollapsibleActionView or/and custom animations (really depends on what you need from it)

  2. use search view, style it without text icon:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:searchViewStyle">@style/appTheme.SearchView</item> </style> <style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView"> <item name="android:searchIcon" tools:targetApi="lollipop">@null</item> </style>

And then basically add icon to right as a normal drawable.

  1. extend search view and inflate custom layout in constructor (haven't tested this one)

Upvotes: 0

Related Questions