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();
I'm targetting API level 8+ and I'd like to have a full width
search view on top of the action bar, expanded by default and not collapsable.
Here's what I did:
I've an ActionBar activity where I set inside onCreate:
final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
then onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(searchItem);
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
MenuItemCompat.expandActionView(searchItem);
return true;
}
Here my search.xml inside menu folder:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
android:title="#string/search_product"
myapp:actionViewClass="android.support.v7.widget.SearchView"
myapp:showAsAction="always">
</item>
</menu>
and my searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_product"
android:label="#string/searchable"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" >
</searchable>
This way, some extra space remains between back button and search widget.
Can you help me whit this?
P.S. Why search icon is not INSIDE the widget? If I set setIconifiedByDefault to
true and then click on the icon, the image remains inside the text space...
I created a new project, targeting api 11 and above. I have an activity where I want a SearchView expanded by default:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setIconifiedByDefault(false);
}
which works, but I don't want any icon or title on the actionbar, just the searchview. I tried getting rid of my title and icon by doing:
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setIcon(android.R.color.transparent);
But my actionbar still has this padding to the left of the searchview:
Is there a way to get rid of it?
Thanks
-------- Edit --------------------
Here's my activity in full:
public class GreatAndroid extends FragmentActivity {
private SearchView mSearchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem mi = menu.findItem(R.id.action_search);
mSearchView = (SearchView) mi.getActionView();
mSearchView.setIconifiedByDefault(false);
return true;
}
}
and here's the menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:title="#string/search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
Even though the icon is transparent, it still takes up the space:
getActionBar().setIcon(android.R.color.transparent);
Remove this line and add:
getActionBar().setDisplayShowHomeEnabled(false);
Now, you should have the SearchView take up all the available space.
Action items that can be collapsed have a max width. The only workaround I can think of is the following:
public class GreatAndroid extends FragmentActivity {
private SearchView mSearchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem mi = menu.findItem(R.id.action_search);
mSearchView = (SearchView) mi.getActionView();
mSearchView.setIconifiedByDefault(false);
// You can use Display.getSize(Point) from API 13 onwards.
// For API 11 and 12, use Display.getWidth()
final Point p = new Point();
getWindowManager().getDefaultDisplay().getSize(p);
// Create LayoutParams with width set to screen's width
LayoutParams params = new LayoutParams(p.x, LayoutParams.MATCH_PARENT);
mSearchView.setLayoutParams(params);
return true;
}
}
Although it looks like there's still space to the left, you can use mSearchView.setBackgroundColor(Color.RED); to see that there really isn't.
Regarding left padding you could try using collapseActionView
Here is an example menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/search_hint"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
</menu>
I could not get #Vikram's awnser to work, but I found that another answer from Stack Overflow:
Open your styles.xml file and add below codes in your Actionbar style
<item name="android:displayOptions">showHome|homeAsUp|showTitle</item>
<item name="displayOptions">showHome|homeAsUp|showTitle</item>
<item name="android:icon">#android:color/transparent</item> <--this do the magic!
p/s: I'm using Actionbar Sherlock and this works just fine
(See Remove icon/logo from action bar on android work)
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();
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: