Android searchView triggering onOptionsItemSelected - android

I'm following this guide to add an actionbar SearchView widget to my activity: http://developer.android.com/training/search/setup.html
I've got it working except on this page: http://developer.android.com/training/search/backward-compat.html
it says that "only versions older than 3.0 call onOptionsItemSelected() when the user selects the search menu item" so I can call onSearchRequested() in onOptionsItemSelected when this search button is pressed for older devices. But when I test that on Jellybean, I press the search button and it expands the searchview but also opens a search dialog over, so I have two search widgets on top of each other.
It seems like onOptionsItemSelected() is still being called when the search button is pressed on a 3.0+ device, but the guide says otherwise. Am I implementing it wrong? I tried making a new project and following the guide exactly, and it has the same behaviour.

This was a misinterpretation on my part. I thought the documentation was saying that the hardware search button would not trigger onOptionsItemSelected and would instead expand the searchView. But it actually just says that the search menu option will not trigger onOptionsItemSelected
To fix this I overrode onSearchRequested and expanded the searchView there

Related

Android ActoinBar homeAsUp only works once

I'm working on an app that only uses a single Activity and switches out fragments as they are needed with the navigation drawer.
Now we want to navigate back from one of these fragments by using the homeAsUp button in the ActionBar.
I have followed all the steps to set the button up. From disabling the navigation drawer setDrawerIndicatorEnabled(false) and calling setDisplayHomeAsUpEnabled(true) in the fragment's onCreateView().
I also set setHomeButtonEnabled(true) in the MainActivity's onCreate() however because the app is already in the MainActivity, we cannot specify a Parent Activity.
Whenever I run a fresh install of the app, the homeAsUp button works and is registered in the onBackPressed(), not onOptionsItemSelected() method. However, when I close the app and run it again the button does not even register clicks.
In onBackPressed() I check a few conditions, but it does not block the button press.
In onOptionsItemSelected() I check for android.R.id.home.
Unfortunately, I cannot post the code.
This post describes what I'm trying to achieve: Switching between Android Navigation Drawer image and Up caret when using fragments
I managed to fix the issue I was experiencing. It was a very simple mistake.
Because I'm not the original author of this code, I went through the MainActivity thoroughly.
As it turns out the original author called setDisplayHomeAsUpEnabled in the onCreate function (which is extremely long), but near the end he also called setSupportActionBar, which made the first call of setDisplayHomeAsUpEnabled useless. Moving setDisplayHomeAsUpEnabled below setSupportActionBar fixed my problem.
If you did everything correctly, make sure that your code is written in the correct order.
Furthermore, if you use custom toolbars in other fragments, remember to set your original Support Action Bar by calling setSupportActionBar(toolbar.find(this)) in onResume of your MainActivity.

Enabling batch contextual actions in a ListView without long-click

I have a ListView with a MultiChoiceModeListener to delete items. At the moment, the action mode is started when a long-click is performed on a item. This is working fine but, in addition, I want to allow the user enable the action mode when they click on a "Edit" button.
Is there a way to enable this mode without a long-click?
I think this is what you are looking for: http://developer.android.com/guide/topics/ui/menus.html#CABforViews
Basically:
Implement the ActionMode.Callback interface
Call startActionMode() manually on button click
An example CommonsWare project that might help: https://github.com/commonsguy/cw-omnibus/tree/master/ActionMode/Manual

When does onCreateOptionsMenu happen in an ActionBar enabled activity?

I know the menu item will be set as action icons in the ActionBar.
I want to know exactly this onCreateOptionsMenu function, when does it called in the activity lifecycle.
From my test, it does not even after onResume
The documentation says the following:
public boolean onCreateOptionsMenu (Menu menu)
Initialize the contents of the Activity's standard options menu. You should place your menu items in to menu. This is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu).
Further explanation here: http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu%28android.view.Menu%29
And quoting what CommonsWare put on another related question:
The onCreate method is called first, and before it finishes onCreateOptionsMenu is called.
That will be true on devices and apps with an official Honeycomb-style action bar. If there is no action bar, onCreateOptionsMenu() should not get called until the user calls up the menu, typically by pressing the MENU button.
Link here: Android: When is onCreateOptionsMenu called during Activity lifecycle?
In my tests I discover that onCreateOptionsMenu is called after onResume as you can see too in this complete diagram of the lifecycle:
https://raw.githubusercontent.com/xxv/android-lifecycle/master/complete_android_fragment_lifecycle.png
I believe it is called at the same time as onCreate, just before the menu appears, in this case the actionbar
This is called the first time you touch the "options" dedicated button.
I'm trying to figure out when it's called on ActionBar supported also.
Also, you can request activity to do it, (but you need a Menu stub implementation)
activity.onCreatePanelMenu(Window.FEATURE_OPTIONS_PANEL, menu);

Can I create a menu button somewhere other than the options menu in android?

I've read through all of the android documentation that I can find but I'm still not clear on whether a button can be added to an application for an android device somewhere outside of an options/context menu. It seems like all of the menu buttons are only accessible either from a tab bar at the bottom or the menu button on the device. Is this correct?
I'm not sure I understand what you mean, but you can use a Button widget in an activity anywhere you want, just like any other widget type. See:
http://developer.android.com/reference/android/widget/Button.html

How to activate option menu when search box is displayed? (SearchManager)

I am trying to build a quick search box. it is basically a widget. Upon clicking, it pops up the quick search box and launch the browser with query string. No activity except searchactivity.
And also added codes for 'recent query suggestion' But having a problem to have menu for 'clear history' Here is what I wanted to implement. When quick search box is displayed, by pressing menu button, I want option menu to be popped up, keypad to be disappeared, quick search box to be stayed.
the implementation of google sample code - Searchable dictionary is not what I want to implement. It starts an activity with instruction message and when a user presses the search button or menu button, it pops up the quick search box. Mine is when it runs from a widget, the quick search box is popped up right away just like the google search widget.
How can I override onCreateOptionMenu on searchmanager? Or is there any way to activate option menu when the searchmanager is activated?
Please take a look at the images below. the second image is what I want to implement upon clicking menu button.
What you need to do is make the search button launch the default search activity when it is pressed. And make it sorta match the existing search dialog style. You can notice this is how the official Twitter client works. You can use apktool (google it) to take a look at the official Twitter clients resource files to get a better idea of how this can be accomplished.

Categories

Resources