Search Widget Full Width - android

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...

Related

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.

How to collapse SearchView on backpressed using appcompat v7.?

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()

Android: menu displaying at screen bottom

I was trying to create a menu, and add some actions "ifRoom" using code below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_search"
android:icon="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/search_description" />
<item
android:id="#+id/menu_refresh"
android:title="#string/menu_refresh"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/menu_preference"
android:title="#string/menu_preferences"
android:showAsAction="never"
/>
</menu>
But what i got is the menu is displaying at the bottom...
I want it to be at top of the screen...
Any idea what happened here? Thanks!
Screenshot link: http://i.stack.imgur.com/knECg.png
Below is the MainActivity.java onCreateOptionaMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
// Moved from onCreate -- Retrieve the Search View and configure/enable it
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()) );
return true;
}

How to animate full width SearchView expand/collapse in action bar?

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();

Change support SearchView theme

In my app I have a SearchView that uses the support.v7 library because of compatibility.
The problem is that, in a device with API 11 or higher, when I open the searchview i get the support api style and only after I search for something the style changes to my application style.
I want the theme in the newest APIs to be always like the second image. Already tried other themes but the result is always the same.
Style when the searchview expands:image link.
Style after a search:image link
options_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:malfriends="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/search"
android:icon="#drawable/ic_action_search"
android:title="#string/search_title"
malfriends:actionViewClass="android.support.v7.widget.SearchView"
malfriends:showAsAction="collapseActionView|ifRoom"/>
<item
android:id="#+id/logoff"
android:title="#string/logout"
malfriends:showAsAction="never"/>
</menu>
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/app_name" android:searchSuggestAuthority=
"com.dandrex.malfriends.controller.provider.SearchFriendsProvider"
android:searchSuggestSelection=" ?" />
My activity class
import android.support.v7.widget.SearchView;
...
public class FeedTabActivity extends ActionBarActivity implements
ActionBar.TabListener
onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(new ComponentName(
FeedTabActivity.this, FeedTabActivity.class)));
searchView.setIconifiedByDefault(false);
} else {
MenuItem itemCompat = menu.findItem(R.id.search);
searchView = (SearchView) MenuItemCompat.getActionView(itemCompat);
}
return true;
}

Categories

Resources