Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SearchView - Move icon to right (without ActionBar)

I have an android SearchView like so -

 <SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Search the forum ..."
        android:iconifiedByDefault="true"
        android:id="@+id/search_forum"
        android:layout_alignParentRight="true"
        android:textColor="#aaa" />

This is not in my ActionBar, it's somewhere else in my activity.

I need the icon (the search icon) to stay on the right of my layout. On click it should expand into the SearchView (same behaviour as the ActionBar SearchView). I've tried android:layout_alignParentRight="true" but the icon stays to the left.

Any help would be great :)

like image 201
Mallika Khullar Avatar asked Oct 13 '15 15:10

Mallika Khullar


3 Answers

I found a solution for this problem. Just set layout Direction to "rtl" and icon will be in Right Side. Here is the code for my search view and it is working as intended.

<SearchView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layoutDirection="rtl"
    android:iconifiedByDefault="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
like image 118
Harish Bhagtani Avatar answered Sep 29 '22 17:09

Harish Bhagtani


I couldn't find an exact answer to this so I did this- Create an image view with search icon. Placed it on the right of my layout. On click, made the search view visible and set iconified false and requested focus for the search view.

I'd be so grateful if somebody could eventually help me find a proper solution for this.

like image 23
Mallika Khullar Avatar answered Sep 29 '22 18:09

Mallika Khullar


If the question is still unanswered:

Just move the SearchView into a RelativeLayout, and align it to the right side of the parent:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.SearchView
    android:layout_width="wrap_content"
    android:id="@+id/searchView"
    android:layout_alignParentRight="true"
    android:layout_height="match_parent" />

The final result looks like this:

CardView with Linearlayout containing aligned SearchView

like image 45
thelbrecht Avatar answered Sep 29 '22 17:09

thelbrecht