android - How to add a button to the right of SearchView ? -


i struggling adding button right of searchview in toolbar. here code of menu corresponding toolbar :

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">  <item     android:id="@+id/change_view"     android:title="change view"     app:showasaction="always"/>  <item     android:id="@+id/search_view"     android:title="@string/search_hint"     app:actionviewclass="android.support.v7.widget.searchview"     app:showasaction="always"/> </menu> 

and oncreateoptionsmenu() :

public boolean oncreateoptionsmenu(menu menu) {     menuinflater inflater = getmenuinflater();     inflater.inflate(r.menu.search, menu);     getsupportactionbar().setdisplayshowtitleenabled(false);     getsupportactionbar().setdisplayhomeasupenabled(true);      searchmanager searchmanager = (searchmanager) getsystemservice(context.search_service);     menuitem searchitem = menu.finditem(r.id.search_view);     searchview searchview = (searchview) searchitem.getactionview();     searchview.setsearchableinfo(searchmanager.getsearchableinfo(getcomponentname()));     searchview.seticonified(false);     searchview.seticonifiedbydefault(false);     searchitem.expandactionview();     imageview searchviewicon = (imageview)searchview.findviewbyid(android.support.v7.appcompat.r.id.search_mag_icon);     searchviewicon.setvisibility(view.gone);     ... } 

it works when add search_view item left of searchview can see below :

button left of searchview

but when try place right of search view :

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">  <item     android:id="@+id/search_view"     android:title="@string/search_hint"     app:actionviewclass="android.support.v7.widget.searchview"     app:showasaction="ifroom"     android:orderincategory="1"/>  <item     android:id="@+id/change_view"     android:title="change view"     app:showasaction="always"     android:orderincategory="2"/> </menu> 

the button doesn't show on toolbar can see below :

trying set button right of searchview

furthermore, tried remove left space between button , searchview because thought there wasn't enough place right show button didn't succeed.

i tried many things in vain. welcome. hope problem sufficiently detailled.

i thank in advance.

try

<menu xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools">      <item         android:id="@+id/action_search"         android:icon="@android:drawable/ic_menu_search"         android:title=""         app:actionviewclass="android.support.v7.widget.searchview"         app:showasaction="ifroom" />      <item         android:id="@+id/change_view"         android:icon="@drawable/ic_mode_edit"         android:title=""         app:showasaction="ifroom" />  </menu> 

no need set grammatically.

result

enter image description here

for reducing padding of search view, can try this. warning though, kind of hack , need tested different android versions. have tested android n.

try {      linearlayout searcheditframe = searchview.findviewbyid(r.id.search_edit_frame);      ((linearlayout.layoutparams) searcheditframe.getlayoutparams()).leftmargin = 0;      edittext searchedittext = searchview.findviewbyid(android.support.v7.appcompat.r.id.search_src_text);      searchedittext.setpadding(0, 2, 0, 2);      } catch (exception e) {       } 

activity/fragment code

@override     public void oncreateoptionsmenu(menu menu, menuinflater inflater) {         inflater.inflate(r.menu.your_menu, menu);         searchview = (searchview) menu.finditem(r.id.action_search).getactionview();         searchview.setqueryhint("search glossary...");         searchview.setonquerytextlistener(this);         super.oncreateoptionsmenu(menu, inflater);     } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -