Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep expanded SearchView on the right side of ActionBar (while using custom icon)?

My question is closely related to SearchView wrong alignment and SearchView positions when expanded in ActionBar but the answers posted there do not work.

So, I'm using a SearchView in the Action Bar:

<item android:id="@+id/action_search"
    android:icon="@drawable/navi_search"
    android:title="@string/action_search"
    android:showAsAction="ifRoom|collapseActionView"
    android:actionViewClass="android.widget.SearchView"
   />

It looks like this by default (top-right corner, next to overflow menu):

enter image description here

Problem is, when expanded, I'd like the SearchView to stay on the right, but instead, it moves to the left edge, next to home button (app icon).

I tried changing showAsAction to either of these (as suggested here):

android:showAsAction="ifRoom"
android:showAsAction="always"

...And that does fix the alignment when expanded, but the custom icon is lost when in collapsed state:

enter image description here

Also, stuff like android:layout_alignParentRight="true" and android:layout_centerInParent="true" on the menu <item> have no effect.

Any ideas?

I'm targetting Android 4.0 and above:

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />
like image 421
Jonik Avatar asked Nov 21 '13 14:11

Jonik


People also ask

How to set SearchView in android studio?

SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class. For making search field visible, SearchView uses setIconifiedByDefault(false) method.

How do I change the Search icon on my Android?

There's a way to do this. The trick is to recover the ImageView using its identifier and setting a new image with setImageResource() . This solution is inspired on Changing the background drawable of the searchview widget. To clarify, this is to change the default icon when the search view is "expanded."

How do I add items to Action Bar?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.


2 Answers

Alright, I found a way:

In onCreateOptionsMenu(), after the standard menu inflating stuff, set ActionBar.LayoutParams with Gravity.RIGHT for the SearchView instance:

@Override
public boolean onCreateOptionsMenu(Menu menu) {        
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);

    // ...

    // Find the SearchView; make it aligned to right, also in expanded mode:
    MenuItem item = menu.findItem(R.id.action_search);
    SearchView search = (SearchView) item.getActionView();
    search.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));
}

(In the menu definition XML, I still use android:showAsAction="ifRoom|collapseActionView".)

Now the SearchView opens on the right, in the same place where the icon is, and the custom icon is still in use.

like image 158
Jonik Avatar answered Sep 19 '22 08:09

Jonik


Try editing this line

android:showAsAction="ifRoom|collapseActionView"

to:

android:showAsAction="ifRoom"

it will align the edittext to the right.

like image 38
alfred abalos Avatar answered Sep 17 '22 08:09

alfred abalos