How to move SearchView 's search Icon to the right side?

meunicorn picture meunicorn · Apr 28, 2016 · Viewed 34.7k times · Source

enter image description here

Here is my layout, using android.support.v7.widget.SearchView. I want to move the search icon to the right side,but I haven't found any method... `

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <android.support.v7.widget.SearchView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_main_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>

`

after i click the icon,it will change to this layout enter image description here

Answer

Aditya Vyas-Lakhan picture Aditya Vyas-Lakhan · Apr 28, 2016

You can do it programmatically

SearchView search = (SearchView) item.getActionView();
    search.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));