How to collapse SearchView on backpressed using appcompat v7.? - android

I am using appcompat v7 for searchview with toolbar except actionbar. below is my menu xml file and 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 i want to collapse searchview if it is expanded otherwise want to work backpressed on backpressed() method. how can i achieve this.?

Collapsing on backpressed is handled by default in my own setup. I didn't implement a custom onBackPressed method. I did nothing special except extending from ActionBarActivity.
I used MenuItemCompat to get actionview, that might do the trick.
This is my 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 is how i create the menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.shop_list, menu);
SearchManager searchManager =
(SearchManager)getSystemService(Context.SEARCH_SERVICE);
//Using MenuItemCompat here, that can do the trick
searchView =
(SearchView)MenuItemCompat.
getActionView(menu.findItem(R.id.search));
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
//etc...
//etc...
return true;
}

In onBackPressed call invalidateOptionsMenu() or store SearchView as Activity field and call searchView.onActionViewCollapsed(); but that can require additional work when restoring your Toolbar state (title state etc.).

Try this inside the Class and extend ActionBarActivity in your Class
(Example: - public class Home extends ActionBarActivity)
#Override
public void onBackPressed() {
super.onBackPressed()
}
For collapsing the SearchView: Try this,
menu.collapseActionView();
(or)
searchView = menuItem.getActionView().
(or)
searchView.onActionViewCollapsed()

Related

Android SearchView widget: setIconified[ByDefault or not](false) not working

I have set up a Toolbar and within, a SearchView widget. The XML of this Toolbar's menu is the following:
<item android:id="#+id/searchView"
android:title="#string/search_title"
android:icon="#drawable/ic_baseline_search_24px"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
On the fragment which has this Toolbar, I have overrided its method onCreateOptionsMenu to display the Toolbar and to configure SearchView.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menu_inflater) {
menu_inflater.inflate(R.menu.action_bar_menu, menu);
super.onCreateOptionsMenu(menu,menu_inflater);
AppCompatActivity app_compat_activity = (AppCompatActivity) getActivity();
SearchView search_view = (SearchView) menu.findItem(R.id.searchView).getActionView();
search_view.setIconified(false);
search_view.setIconifiedByDefault(false);
search_view.clearFocus();
SearchManager search_manager = (SearchManager) Objects.requireNonNull(app_compat_activity).getSystemService(Context.SEARCH_SERVICE);
search_view.setSearchableInfo(Objects.requireNonNull(search_manager).getSearchableInfo(app_compat_activity.getComponentName()));
}
My aim is that the SearchView expands (filling all the Toolbar's width). The above code doesn't work.
Change
app:showAsAction="collapseActionView|ifRoom"
to
app:showAsAction="always"
Remove search_view.clearFocus();

Wrong menuitem found when fragment changes in Android

I have the following menu in the main fragment that contains a list:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always|collapseActionView"
android:icon="#android:drawable/ic_menu_search"
android:id="#+id/menu_item_search"
app:actionViewClass="android.widget.SearchView"
android:title="Search"
android:iconifiedByDefault="true"/>
</menu>
It has the necessary setting in the onCreate:
setHasOptionsMenu(true);
Its onCreateOptionsMenu:
getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu,inflater);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) ((MenuBuilder)menu).getActionItems().get(0).getActionView();
if(searchView!=null)
{
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setSubmitButtonEnabled(false);
searchView.setOnQueryTextListener(this);
}
When I click an item, a new fragment opens and it has a different menu:
<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"
tools:context=".MainActivity">
<item android:id="#+id/action_Save"
android:title="#string/action_Save"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#android:drawable/ic_menu_save"
/>
</menu>
Its onCreateOptionsMenu:
getActivity().getMenuInflater().inflate(R.menu.menu_fragment_second, menu);
super.onCreateOptionsMenu(menu, inflater);
If I press Back:
if (getFragmentManager().getBackStackEntryCount() > 0)
getFragmentManager().popBackStack();
my first fragment opens and tries to find its SearchView.
It can't find it as the menu of the second fragment is checked and it finds only the Save item.
What should I do to make it work?
You need to call invalidateOptionsMenu() to reset your menu for the fragment.
I had to change how the menuitem was found.
Instead of this:
SearchView searchView = (SearchView) ((MenuBuilder)menu).getActionItems().get(0).getActionView();
I use now this:
SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
This way it works.

Android: strange SearchView's behavior

I'm trying to implement SearchView according to some online tutorials. But when i clicked the search icon, it did not expand, instead another search icon appeared to the left and i must click again; this time it worked.
Are there something i'm missing here?
Here are the captured images, sorry i can't post these to stackoverflow yet.
http://1drv.ms/186yI2Y
If i delete collapseActionView then it worked, but i can't customize search icon.
<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" tools:context=".MainActivity">
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_action_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.widget.SearchView"/>
</menu>
In MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchView.setIconifiedByDefault(true);
mSearchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showResultsFragment();
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean toggleHandled = mDrawerToggle.onOptionsItemSelected(item);
return toggleHandled || super.onOptionsItemSelected(item);
}
Thank in advance!
in your menu xml i changed app:actionViewClass="android.support.v7.widget.SearchView"
<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" tools:context=".MainActivity">
<item android:id="#+id/search"
android:title="wrwrwr"
android:icon="#drawable/abc_ic_menu_copy_mtrl_am_alpha"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
and in your activity
import android.support.v7.widget.SearchView; //don't import android.widget.SearchView

Android Action bar only showing in overflow

In my android app .. I am trying t put a search edittext with seARCH icon .. but I cant see the search icon in action bar rather the title is seeing in overflow. I am giving my code below
activity
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"/>
<item
android:id="#+id/action_settings"
android:title="Settings"/>
</menu>
MainActvity
public class MainActivity extends ActionBarActivity
{
WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv1 = (WebView) findViewById(R.id.wView1);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.loadUrl("http://www.example.com/songs/");
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
}
Use Search Manager n search view in onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
and in your main.xml add this item for search
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"/>
I think you must return `true` instead of
super.onCreateOptionsMenu(menu)
in `onCreateOptionsMenu(Menu menu)` method
Excuses. That was wrong.
I think this problem has something to do with the base class you are using. Your are subclassing ActionBarActivity instead of Activity. This means that you are using compatibility for previous versions. For this to work, you need to set your application's Theme to Theme.AppCompat (or a similar one), and also include the Support Library. Look here
I just realized how to fix the problem!
your menu xml file should have an starting tag like:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myApp="http://schemas.android.com/apk/res-auto" >
you can replace
myApp
with any names you want, NOW the important part is when you define an action item:
<item
android:id="#+id/actionitem_switch_view"
android:title="#string/view_as"
android:showAsAction="always"
myApp:showAsAction="always"
android:icon="#drawable/ic_action_view_as_list"/>
You'd have two showAsAction tags,one for API<11 devices and one for the others,
Look closely that the two showAsAction tags are different,one is from Android namespace and the other's from the tag you defined in menu Starting point

How to make the Action Bar SearchView fill Entire action bar?

I was trying to create an action bar search in my application, but in the expanded state the SearchView is not taking the entire action bar width( it is still showing other actions)!
So, how to make the SearchView fill the full ActionBar Area (as in GMAIL app)?
None of the above solutions worked for me. Setting the MaxWidth of the SearchView worked for me:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
Got it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_swipe_tabbed, menu);
MenuItem searchViewItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchViewItem.getActionView();
searchView.setIconifiedByDefault(false);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
searchView.setLayoutParams(params);
searchViewItem.expandActionView();
return true;
}
<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"
tools:context=".MySwipeTabbedActivity" >
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView"
/>
<item android:id="#+id/action_share"
android:title="Share"
android:icon="#android:drawable/ic_menu_share"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="Settings"
android:icon="#android:drawable/ic_menu_preferences"
android:showAsAction="never" />
</menu>
[Optional] Avoid the searchView from collapsing:
searchViewItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false; //never collapse
}
});
The Answer given by #Hiep is correct i followed those steps to get my solution.but i was using ActionbarCompact lib so it take some changes to made so that i get it to work This solution is only the modified answer of Hiep if you are using appcompact
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:title="search"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
materialdesign:showAsAction="always|collapseActionView"
materialdesign:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
And in the Main Class onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_swipe_tabbed, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
searchView.setLayoutParams(params);
searchView.setIconified(false);
MenuItemCompat.expandActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
Avoid the searchView from collapsing:If u use the above method in Appcompact it will create a crash so use this solution to avoid that.
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
/* (non-Javadoc)
* #see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionExpand(android.view.MenuItem)
*/
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
/* (non-Javadoc)
* #see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionCollapse(android.view.MenuItem)
*/
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
Thank you
Try This Only will work and keep all other items set to ifRoom
<item
android:id="#+id/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="#string/action_search"
app:showAsAction="always"/>
Yes, I'm really late :)
But I would like to share one more alternative solution. The idea is just to hide the grouped menu items when the SearchView gets expanded.
Step 1:
Group your menu items. Please make sure showAsAction of search view item is app:showAsAction="collapseActionView|always"
<group android:id="#+id/myMenuGroup">
<item
android:id="#+id/actionSearch"
android:icon="#drawable/ic_search"
android:title="#string/search"
app:actionViewClass="android.widget.SearchView"
app:iconTint="#color/textColorVariant2"
app:showAsAction="collapseActionView|always" />
<item
android:id="#+id/actionFilter"
android:icon="#drawable/ic_filter"
android:orderInCategory="0"
android:title="#string/filter"
app:iconTint="#color/textColorVariant2"
app:showAsAction="ifRoom" />
..... </group>
Step 2: Get the menu reference in onPrepareOptionsMenu() callback
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
super.onPrepareOptionsMenu(menu)
this.menu = menu // this.menu is a global scoped variable
return true
}
Step 3
Add MenuItem.OnActionExpandListener into your activity and implement onMenuItemActionExpand() and onMenuItemActionExpand() like below
override fun onMenuItemActionExpand(p0: MenuItem?): Boolean {
menu?.setGroupVisible(R.id.myMenuGroup, false)
return true
}
override fun onMenuItemActionCollapse(p0: MenuItem?): Boolean {
menu?.setGroupVisible(R.id.myMenuGroup, true)
return true
}
This approach will expand the SearchView completely inside the ActionBar even if there are other menu icons present

Categories

Resources