I'm developing an application where the user presses the "Search" icon in the ActionBar and a SearchView is made visible at the top of the screen.
My problem is that the SearchView is not in focus nor expanded so the user has to press the search button on the Searchview to make it expand and bring out the keyboard.
How should this be solved?
You can also call to expandActionView() method in order to force it:
#Override
public boolean onCreateOptionsMenu( Menu menu )
{
super.onCreateOptionsMenu( menu );
MenuItem searchMenuItem = menu.findItem( R.id.mi_search ); // get my MenuItem with placeholder submenu
searchMenuItem.expandActionView(); // Expand the search menu item in order to show by default the query
return true;
}
Search item in the Action Bar layout:
<item
android:id="#+id/mi_search"
android:icon="#drawable/abs__ic_search_api_holo_light"
android:title="#string/search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
/>
To make the SearchView expanded by default, call setIconifiedByDefault(false) on it when you initialise it (e.g. in onCreateOptionsMenu(..) or onPrepareOptionsMenu(..)). I've found in most cases this will give it focus automatically, but if not simply call requestFocus() on it too.
If you want to have it iconifiedByDefault, this worked for me. setFocusable and setIconified are needed.
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setIconifiedByDefault(true);
searchView.setFocusable(true);
searchView.setIconified(false);
searchView.requestFocusFromTouch();
Update: If you are Using android.support.v7.widget.SearchView the behaviour us very different. clearFocus is needed if you don't want the keyboard pop-up all the time. For some reason the menu is recreated all the time, when using appcompat.
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setIconified(false);
searchView.clearFocus();
If you're using it in layout, you can call
mSearchView.onActionViewExpanded()
This worked for me :
In the root layout :
xmlns:app="http://schemas.android.com/apk/res-auto"
SearchView defined as follows :
<android.support.v7.widget.SearchView
android:id="#+id/search_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp"
android:background="#drawable/search_view"
app:iconifiedByDefault="false"
app:queryHint="Search name or email"
>
<requestFocus />
</android.support.v7.widget.SearchView>
The difference is with app tag.
app:iconifiedByDefault="false"
app:queryHint="Search name or email"
If you are using inside an activity you need to use
view.onActionViewExpanded();
if you are using inside menu options you need to use
MenuItem.expandActionView();
Note: it works only for SearchView
these two situations are worked for me.
This worked for me:
menu.expandActionView();
You can use the SearchView#setIconified() method on a SearchView handle in your Java code. More here:
http://developer.android.com/reference/android/widget/SearchView.html#setIconified(boolean)
You can also make use of SearchView#setIconifiedByDefault(). More info here:
http://developer.android.com/reference/android/widget/SearchView.html#setIconifiedByDefault(boolean)
use this
SearchManager searchManager = (SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = new SearchView(mActivity.actionBar.getThemedContext());
searchView.setSearchableInfo(searchManager.getSearchableInfo(mActivity.getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setQueryHint("Search");
menu.findItem(R.id.action_search).setActionView(searchView);
#Pascalius's answer worked for me. But every time you close the SearchView, and click again, you lost the Focus. So I inserted the code in a setOnMenuItemClickListener like this:
MenuItem item = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) item.getActionView();
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
searchView.setIconifiedByDefault(true);
searchView.setFocusable(true);
searchView.setIconified(false);
searchView.requestFocusFromTouch();
return false;
}
});
For Appcompat Searchview you can use this method:
MenuItemCompat.expandActionView(mSearchMenuItem);
Kotlin
var searchView = menu.findItem(R.id.action_search).actionView as SearchView
searchView.isIconified = true
Call expandActionView() on the menuItem.
menuItem.expandActionView()
I was having a hard time doing this with SearchView widget, but the expandActionView() method is actually defined in the MenuItem class, not SearchView class. So, I solved it by
MenuItem searchItem = menu.findItem(R.id.item_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchItem.expandActionView();//the method expandActionView is called on searchItem not searchView
MenuItemCompat's SearchView has a property named maxWidth.
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setMaxWidth(xxx);
use screen width instead of xxx offcourse
I am using android.widget Searchview and iconified by default.Below code in xml helped me make it expand and autofocus on search view,when clicked:
<SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:queryHint="Search"/>
<item
android:id="#+id/search"
android:icon="#drawable/ic_search"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always"/>
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setQueryHint("Search the customer...");
searchView.setSearchableInfo(Objects.requireNonNull(searchManager).getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setIconifiedByDefault(false);
searchView.requestFocus();
}
<pre>
<item
android:id="#+id/search"
android:icon="#drawable/ic_search"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always"/>
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setQueryHint("Search the customer...");
searchView.setSearchableInfo(Objects.requireNonNull(searchManager).getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setIconifiedByDefault(false);
searchView.requestFocus();
}
</pre>
android:iconifiedByDefault="true"
Above is the code in XML helped me make it expand and autofocus on search view when clicked:
Related
I'm trying to fetch my SearchView from the Toolbar within a Fragment.
Im infalting a menu item in the onCreateOptionsMenu.
Problem : searchItem.getActionView() returns a "View" and not a "SearchView". See code below
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.menu_search, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = searchItem.getActionView(); // This one is red - "returns View, should return Searchview"
}
XML for menu item (menu_search.xml):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="#string/search"
android:id="#+id/search"
android:icon="#drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Try this :
MenuItem search = menu.findItem(R.id.search);
SearchView searchView = (SearchView) search.getActionView();
I already define setIconified() and setIconifiedByDefault() to false but the SearchView menu item is not expanding by default. Here's how I implemented it:
View customTitle = getLayoutInflater().inflate(R.layout.toolbar_custom_title, null);
toolbar.inflateMenu(R.menu.menu_buddies);
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
SearchView searchView = (SearchView) toolbar.getMenu().findItem(R.id.menu_search).getActionView();
searchView.setQueryHint("Search Buddies");
searchView.setIconified(false);
searchView.setIconifiedByDefault(false);
My menu_buddies:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
xmlns:pawesome="http://schemas.android.com/apk/res-auto"
android:title="#string/action_search"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
pawesome:showAsAction="always|collapseActionView"
pawesome:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
You need to change the value of android:showAsAction to always. The SearchView's attribute android:iconifiedByDefault should be true.
Have you solved this yet?
Remove:
searchView.setIconified(false);
and keep only this line:
searchView.setIconifiedByDefault(false);
As well as showAsAction="always" in menu-xml.
This works for me, the searchview is expanded and can not be collapsed by any means.
None of the above answers worked for me.
Use this
SearchManager searchManager = (SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = new SearchView(mActivity.actionBar.getThemedContext());
searchView.setSearchableInfo(searchManager.getSearchableInfo(mActivity.getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setQueryHint("search");
menu.findItem(R.id.action_search).setActionView(searchView);
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()
I am using the SearchView from the support.v7 library. I looked at this post which suggests that you can simply animate a search view by setting the LayoutTransition
LinearLayout searchBar = (LinearLayout) searchView.findViewById(searchBarId);
//Give the Linearlayout a transition animation.
searchBar.setLayoutTransition(new LayoutTransition());
However the above does not work for me. The search view also seems to only fill part of the screen width in landscape . I would like for it fill_parent
I was trying to solve both of these problems by creating a simple animation but failed at searchView.getLayoutParams(); because it seems to always return null.
The end goal is to have a SearchView in the action bar that expands/collapses smoothly and takes up the width of the screen width in landscape.
Is there a way to accomplish that?
Here is the search view setup that I currently have:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.menu_main, menu);
//Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
//Returns null
//searchView.getLayoutParams();
return true;
}
And menu_main.xml
<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="Search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
Do in oncreateoptions:
MenuItem searchitem = menu.findItem(R.id.search);
SearchView search= (SearchView) searchitem.getActionView();
ActionBar.LayoutParams p= new ActionBar.LayoutParams(ActionBar.LayoutParams.MATch_parent, same here)
search.setLayoutParams(p);
searchitem.expandActionView();
I would like to create an always-expanded search window in my action bar. I am using the ActionBar Sherlock library. According to some samples I have found, this code should do the trick
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = this.getSupportMenuInflater();
inflater.inflate(R.menu.action_bar, menu);
MenuItem searchItem = menu.findItem(R.id.action_bar_search);
SearchView searchView = (SearchView) searchItem.getActionView(); //returns null
searchView.setIconifiedByDefault(false);
return true;
}
however, getActionView(); returns null.
Is there some other way to get to the searchView, so that I can call the setIconifiedByDefault(false); method ?
I would like to keep the definition of the searchView in the xml file as follows
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_bar_search"
android:title="#string/action_bar_search"
android:icon="#drawable/ic_magnify"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
Thanks for any help
Your issue is probably with your XML. Change android:actionViewClass="android.widget.SearchView" to android:actionViewClass="com.actionbarsherlock.widget.SearchView"
Are you sure that you're using correct R.menu... link? Also try to use getMenuInflater(); instead of getSupportMenuInflater();