android - How to collapse SearchView on backpressed using appcompat v7.? -
i using appcompat v7
searchview toolbar except actionbar. below menu xml file , java file.
menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/search" app:actionviewclass="android.support.v7.widget.searchview" app:showasaction="always"/> <item android:id="@+id/action_sort" android:icon="@drawable/ic_action_sort" android:title="@string/sort" app:showasaction="ifroom"/> </menu>
java file:
@override public boolean oncreateoptionsmenu(menu menu) { menuinflater menuinflater = getmenuinflater(); menuinflater.inflate(r.menu.dashboard, menu); menuitem searchitem = menu.finditem(r.id.action_search); searchmanager searchmanager = (searchmanager) mainactivity.this.getsystemservice(context.search_service); if (searchitem != null) { searchview = (searchview) searchitem.getactionview(); } if (searchview != null) { searchview.setsearchableinfo(searchmanager.getsearchableinfo(mainactivity.this.getcomponentname())); } return super.oncreateoptionsmenu(menu); }
now want collapse searchview if expanded otherwise want work backpressed on backpressed()
method. how can achieve this.?
collapsing on backpressed handled default in own setup. didn't implement custom onbackpressed method. did nothing special except extending actionbaractivity.
i used menuitemcompat actionview, might trick.
this menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="com.yourapp.youractivity"> <item android:id="@+id/search" android:title="@string/app_name" android:icon="@drawable/nav_search" app:showasaction="ifroom|collapseactionview" app:actionviewclass="android.support.v7.widget.searchview" /> </menu>
this how create menu
@override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.shop_list, menu); searchmanager searchmanager = (searchmanager)getsystemservice(context.search_service); //using menuitemcompat here, can trick searchview = (searchview)menuitemcompat. getactionview(menu.finditem(r.id.search)); searchview.setsearchableinfo( searchmanager.getsearchableinfo(getcomponentname())); //etc... //etc... return true; }
Nice articel, This article help me very well. Thank you. Also please check my article on my site Know All About Htaccess Tutorial.
ReplyDeleteIn link article we will learn about How to use .htaccess? The Htaccess document is placed in a directory on the web server. When done, it will be deployed from the Apache web server when a request is made from that directory.