I'm wondering how to do that : when the user press hardware search button, it's open a search view in the actionbar.
Basically I have an activity hosting fragments.
One fragment add to the ActionbarSherlock a searchview, it's working fine:
#Override
public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
MenuInflater inflater) {
if(searchView==null)
searchView = new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext());
searchView.setQueryHint(getString(R.string.search));
searchView.setOnQueryTextListener(this);
menu.add(Menu.NONE, R.string.search, Menu.NONE, R.string.search)
.setIcon(R.drawable.abs__ic_search_api_holo_light)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
super.onCreateOptionsMenu(menu, inflater);
}
I think I can also catch the hardware key in the FragmentActivity with
#Override
public boolean onSearchRequested() {
//DO SOMETHING
return super.onSearchRequested();
}
But I do not see how to open the searchview when the hardware search button is pressed.
Any hint ?
Thanks :).
So, here how I managed to use the hardware search key to open the searchview in the sherlock actionbar :
In my SherlockFragmentActivity :
private Menu ABSMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
ABSMenu = menu;
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onSearchRequested() {
MenuItem mi = ABSMenu.findItem(R.string.search); // R.string.search is the id of the searchview
if(mi!=null){
if(mi.isActionViewExpanded())
{
mi.collapseActionView();
}else
{
mi.expandActionView();
}
}
return super.onSearchRequested();
}
You can expand the search view and collapse it like
searchView.setIconified(false);
searchView.setIconified(true);
This way you can programatically invoke search view
Related
I have MainActivity and it's have fragment. I added toolbar from MainActivity;
MainActivity
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mToolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
When I was open any fragment from MainActivity then that fragment use own menu file
In fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.custom_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
As you can see above I inflated custom menu in onCreateOptionsMenu. But It doesn't work.
after
toolbar.getMenu().clear();
add toolbar.inflateMenu(R.menu.menu_blank);
and I am not set toolbar as acionbar,Maybe you should user getActionbar to do something.
I'm not entirely sure this is what you're asking but I think you want to be able to change the options in the menu, dynamically. You can do the following
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
if (mHideSomething) {
MenuItem myItem = menu.findItem(R.id.action_something);
myItem.setVisible(false);
} //otherwise it will show as usual
return true;
}
Then when you want to change something in the menu...
mHideSomething = true;
supportInvalidateOptionsMenu();
EDIT
Now I understand you're just adding odd behaviour when overriding. You can still do the following (although it seems like these items just shouldn't be part of main menu if they're not relevant always)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.custom_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
MenuItem mainMenuItemToRemove = menu.findItem(R.id.action_something);
mainMenuItemToRemove .setVisible(false);
}
The only problem with the above is it makes assumptions about what is available in the main menu even though the fragment should be reusable. A better solution would be to pass in an interface to the fragment to call back to the activity and let the activity have control. Better still, update the activity logic and never inflate the main menu at all if not required.
I would put a number on the button that represent the numbers of notify like facebooks app.
I follow the first example in this topic :
Actionbar notification count icon (badge) like Google has
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.badge);
MenuItemCompat.setActionView(item, R.layout.feed_update_count);
notifCount = (Button) MenuItemCompat.getActionView(item);
notifCount.setText(String.valueOf(SingletonGrafica.getNumeroNotifica()));
return super.onCreateOptionsMenu(menu);
}
When I call :
MenuItem item = menu.findItem(R.id.badge);
I obtain NullPointer Exception.Anyone can help me?
You need to create the menu first:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the navigation_menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation_menu, menu);
return true;
}
and if you want to update the menu you should do it in:
onPrepareOptionsMenu
if you want the onPrepareOptionsMenu function to be called again you can call invalidateOptionsMenu();
You can also add item from code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add("Badge");MenuItemCompat.setActionView(item, R.layout.feed_update_count);
return true;
}
I have SearchView in the top of the layout (not in the action bar), is there anyway to force this View to be always expanded (opened)?
If not, i wish to place fancy image near it, is there anyway to make SearchView hide this image when expanded (clicked/expanded)?
You can use the property android:iconifiedByDefault="false" on XML or set programatically setIconifiedByDefault(false). Acording to the documentation this property set the SearchView expanded like you want.
Take a look at SearchView.setIconifiedByDefault(boolean iconified)
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchItem.expandActionView();
and in menu file, use
showAsAction="ifRoom|collapseActionView"
If any query, feel free to comment.
I am doing this into fragment ,
i done it by using
searchItem.expandActionView();
but when user press back button it collapse so in collapse method i used
getActivity().getSupportFragmentManager().popBackStack();
// when it collapsed i am going back
Below is my solution in detail:
search_menu.xml
<?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/search_contacts"
android:title="search"
android:icon="#drawable/search2"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" >
</item>
</menu>
in fragment i used below code :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_menu, menu);
// Define the listener
MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item)
{
getActivity().getSupportFragmentManager().popBackStack();
// Do something when action item collapses
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item)
{
// Do something when expanded
return true; // Return true to expand action view
}
};
MenuItem searchItem = menu.findItem(R.id.search_contacts);
// Assign the listener to that action item
MenuItemCompat.setOnActionExpandListener(searchItem, expandListener);
// Any other things you have to do when creating the options menu…
SearchView searchView =
(SearchView) MenuItemCompat.getActionView(searchItem);
//searchView.setIconifiedByDefault(false);
searchItem.expandActionView(); // when fragment opens it expanded auto
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText)
{
seachViewFunction(newText); // in searchViewFunction(newText); i am doing my things
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
You can try to use
searchView.onActionViewExpanded();
and if you do not need keyboard opened
searchView.clearFocus();
I have a sherlockactivity that implements an onItemClickListener. Then I create a new ListView and put data in it, I want to create a search at the top in the actionbarsherlock like a lot of famous apps.
This is my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
SearchView searchView = new
SearchView(getSupportActionBar().getThemedContext());
menu.add("Search")
.setActionView(searchView)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
currently, the word "search" appears at the top of my actionbarsherlock. If I click on it, a textfield will appear in the actionbarsherlock. But when I type anything or click search (In the keyboard) nothing happens. What am I missing?
These two are the methods you need to override to control when the user types on the SearchView and when the user clicks on the search button:
public class MyClass extends SherlockActivity implements SearchView.OnQueryTextListener{
...
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
Then you need
searchView.setOnQueryTextListener(this);
I need to add accessibility to the menu button (physical menu button or non) How can I add accessibility to this component?
Something in here?
public boolean onMenuOpened(int featureId, Menu menu) {
onCreateOptionsMenu(menu);
return super.onMenuOpened(featureId, menu);
}
Thanks!
As stated by CommonsWare in the comments you just have to setTitle on your MenuItem entries. For example during inflation like:
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
menu.getItem(0).setTitle(R.string.first_label_for_menu_item);
menu.getItem(1).setTitle(R.string.second_label_for_menu_item);
}