SearchView in OptionsMenu not full width - android

I have a working SearchView which expands in my OptionsMenu when the user taps on the search icon. However it only expands within the available space among the other OptionsMenu icons. On a wide screen this is fine, but with a narrow space there is only room to show 5-10 charaters in the search box. I want it to overlay the other icons such as it does for the Android Contacts app. Currently, I'm building with targetSdkVersion = 17. Hopefully I'm missing something simple :)
(Note added later: the only solution I've found workable so far is to hide all the menu icons when I want to expand the search icon. This is conceptually simple. But it is messy because when restoring hidden icons, one has to go through a bunch of logic to figure out which ones to restore, or keep state variables around, etc.)
Here's my item xml in for the OptionsMenu:
<item
android:id="#+id/menu_search_shallow"
android:title="Search Current Folder"
android:icon="#drawable/ic_btn_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
I also have in my main activity code:
#Override
public boolean onCreateOptionsMenu (Menu menu)
{
getMenuInflater().inflate(R.menu.nav_menu, menu);
this.optionsMenu = menu;
MenuItem searchItem = menu.findItem (R.id.menu_search_shallow);
searchItem.setOnActionExpandListener (this);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint (getString (R.string.search_shallow_hint));
searchItem = menu.findItem (R.id.menu_search_deep);
searchItem.setOnActionExpandListener (this);
searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint (getString (R.string.search_deep_hint));
}
and
#Override
public boolean onMenuItemActionExpand(MenuItem item)
{
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener (this);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item)
{
SearchView searchView = (SearchView) item.getActionView();
searchView.setQuery ("", false);
return true;
}

Instead of creating a new layout I set the MaxWidth programmatically in onCreateOptionsMenu():
#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);
....

For some reason jjung's solution didn't work for me. I had to add android:maxWidth="10000dp", just a really high maxWidth, to get it to expand to the full available width.
Also, just to add, I'm using the support library (v7), and so my layout file looks like this:
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxWidth="10000dp" />
And in my item, I have:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item
...
myapp:actionLayout="#layout/searchview" />
...
Where myapp is just some arbitrary namespace for the attributes added by the support library (see Adding Action Items for more details).

Maybe I am late but thought it might be helpful for someone who faced the same issue as me.
final SearchView searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);//set search menu as full width
in onCreateOptionsMenu
of your class may solve this problem but one thing we need to understand.
If you just want to show search menu (on clicking of search button) and not any other menu items in actionbar then you need to keep the search item as a first item in menu file. Here is an example
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--keeping searach as first item-->
<item
android:id="#+id/action_search"
android:icon="#drawable/search_ab"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom" />
<item
android:id="#+id/itemFilter"
android:icon="#drawable/filter"
android:title="#string/filter"
app:showAsAction="ifRoom" />
</menu>
And it will be displayed as
on clicking search it will be shown as full width
And if you keep search as second or last item in menu then it will be show like this.
and on clicking search

As crazy as it might seem, in my case problem was this line:
app:actionViewClass="android.widget.SearchView" in menu.xml:
<item
android:id="#+id/action_search"
app:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_search"
android:title="#string/title"
app:actionLayout="#layout/layout_search"
app:showAsAction="always" />
When I removed app:actionViewClass, it fully expanded as defined in my layout_search file.
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxWidth="10000dp"
android:layout_centerInParent="true"
android:background="#color/colorAccent"
android:queryHint="Search me" />

I had a similar issue, that the search bar did not fill the whole width,( but I had no other icons).
I solved it by adding in item:
android:actionLayout="#layout/my_search_view"
and in layout/my_search_view.xml :
<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

There are so many ways to solve this issue - I'll just add the one I've used in several apps.
I have my custom SearchView (with some other customization) which extends android.support.v7.widget.SearchView and in order to stretch to full width:
#Override
public void setLayoutParams(final ViewGroup.LayoutParams params) {
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
super.setLayoutParams(params);
}

Related

Force user to enter number in a SearchView

I am using a SearchView on action bar in my app and I defined it as follows
<?xml version="1.0" encoding="utf-8"?>
<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:title="#string/search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="ifRoom"
app:actionViewClass="android.widget.SearchView" />
</menu>
If it was editText, I could simply write android:inputType="Number" or something like that to force the user to enter number but I could not find such an option for an item. I do not want to check manually if the user entered a number to search view, I want to show the user a keyboard that contains only numbers. How can I accomplish this?
I found an easy solution for this. The only thing one needs to do is to use the method setInputType() inside onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.music, menu);
SearchView searchView= (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setInputType(InputType.TYPE_CLASS_NUMBER);
return true;
}

Search Actionview not collapsing in single up button click android actionbar compat

I am using actionbar compat support library to provide search widget in Action Bar. The search action view is collapsing only after pressing back button or up button twice. I had same problem while executing the sample dictionary search also.
Code in my search activity which extends ActionBarActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu
getMenuInflater().inflate(R.menu.menu_main, menu);
// Find the search item
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
My Searchable.xml looks like this
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/search_label"
android:hint="#string/search_hint"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
>
Let me know if any more information needed...
Check the API guide http://developer.android.com/guide/topics/ui/actionbar.html#ActionView This is what it should look for anything less than API 11.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
yourapp:orderInCategory="9"
yourapp:menuCategory="system"
yourapp:showAsAction="always|collapseActionView"
yourapp:actionViewClass="android.widget.SearchView"
android:title="#string/action_searchSt" />
</menu>
That's probably more attributes than you need (menuCategory and orderInCategory are unnecessary). But assuming you've added in the support library correctly and you read the link I gave, things should work as intended.

How to keep expanded SearchView on the right side of ActionBar (while using custom icon)?

My question is closely related to SearchView wrong alignment and SearchView positions when expanded in ActionBar but the answers posted there do not work.
So, I'm using a SearchView in the Action Bar:
<item android:id="#+id/action_search"
android:icon="#drawable/navi_search"
android:title="#string/action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView"
/>
It looks like this by default (top-right corner, next to overflow menu):
Problem is, when expanded, I'd like the SearchView to stay on the right, but instead, it moves to the left edge, next to home button (app icon).
I tried changing showAsAction to either of these (as suggested here):
android:showAsAction="ifRoom"
android:showAsAction="always"
...And that does fix the alignment when expanded, but the custom icon is lost when in collapsed state:
Also, stuff like android:layout_alignParentRight="true" and android:layout_centerInParent="true" on the menu <item> have no effect.
Any ideas?
I'm targetting Android 4.0 and above:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />
Alright, I found a way:
In onCreateOptionsMenu(), after the standard menu inflating stuff, set
ActionBar.LayoutParams with Gravity.RIGHT for the SearchView instance:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
// ...
// Find the SearchView; make it aligned to right, also in expanded mode:
MenuItem item = menu.findItem(R.id.action_search);
SearchView search = (SearchView) item.getActionView();
search.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));
}
(In the menu definition XML, I still use android:showAsAction="ifRoom|collapseActionView".)
Now the SearchView opens on the right, in the same place where the icon is, and the custom icon is still in use.
Try editing this line
android:showAsAction="ifRoom|collapseActionView"
to:
android:showAsAction="ifRoom"
it will align the edittext to the right.
Using this worked for me:
final SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setOnSearchClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
searchView.setLayoutParams(new Toolbar.LayoutParams(Gravity.END));
}
});

SearchView taking all the space in the new ActionBarCompat

I switched from ActionBarSherlock to ActionBarCompat (support library v7). After some adjustments, almost everything is working fine by now.
But I'm in trouble with the SearchView in the ActionBar. When it's expanded (actually, It's always expanded in my Activity), it takes up all the space and doesn't respect the space of other Action Items that are set to show always (showAsAction="always").
To simulate the problem, use this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_buscar"
app:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/abc_ic_search"
app:showAsAction="always"
android:title="#string/buscar"/>
<item android:id="#+id/tentar_novamente"
android:title="#string/tentar_novamente"
android:icon="#drawable/acao_tentar_novamente"
app:showAsAction="always" />
</menu>
In the Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.busca_action_menu, menu);
searchMenuItem = menu.findItem(R.id.menu_buscar);
searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.setIconifiedByDefault(false);
searchView.setQueryHint(stringBusqueArtistasMusicasEAlbuns);
return super.onCreateOptionsMenu(menu);
}
This is the result in the android 4.3:
And this is the result in the android 2.3 (the action items doesn't even appear):
The same problem happens when I use:
setSupportProgressBarIndeterminateVisibility(true);
The progress indicator appears very strange in the Android 4.3 and doesn't appear in Android 2.3.
The same code worked as expected with ActionBarSherlock. When there was some action item, the SearchView used to decrease its width to give space for the action items.
UPDATE:
I've posted an issue in the Android's Bug Tracker:
https://code.google.com/p/android/issues/detail?id=58251&thanks=58251&ts=1375191632
If you have the same problem, please follow the issue.
UPDATE:
I've tried to put the action items before the SearchView and this way they items appear.
But, the setSupportProgressBarIndeterminateVisibility(true) is still not showing the progress bar.
This is definitely a bug about Android but a workaround can be including SearchView programmatically like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setIconifiedByDefault(false);
getActionBar().setCustomView(searchView);
getActionBar().setDisplayShowCustomEnabled(true);
}
You can also use a layout XML to define SearchView properties.
However "iconifiedByDefault" in XML tends to be ineffective in my experience. (This may my bad though)
Thanks for creating an issue about this.
Here's the URL to the related bug report:
https://code.google.com/p/android/issues/detail?id=58251
Despite what is mentioned in the bug report, my experience was the same with both ActionBarSherlock and ActionBarCompat. So I expect that ActionBarSherlock users are also affected.
Have you tried using collapseActionView()?
I use it like this:
public static MenuItem msearchMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
msearchMenuItem = menu.findItem(R.id.search);
return true;
}
public static MenuItem getSearchMenuItem() {
return msearchMenuItem;
}
public void doSomething(){
//Collapse the SearchBar
getSearchMenuItem().collapseActionView();
}
I don't know if it works with v7, but it certainly works with v4.
Try changing android:showAsAction="collapseActionView|ifRoom"
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
Remove the line
searchView.setIconifiedByDefault(false);
Or you can explicitly call the method with true as the argument.

Android 3.0 honeycomb search view on action bar

How to have a 'search item' on the action bar for honeycomb? If possible please provide full code with layout.
I'm assuming you mean adding the search widget to the actionbar?
Here's how it's done:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
android:title="Search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
This is of course placed in the res/menu folder.
You add this to your actionbar just like any adding any other menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Set appropriate listeners for searchView
...
return super.onCreateOptionsMenu(menu);
}
This was all taken from here: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

Categories

Resources