How to have onSearchRequested not open a web browser - android

I have a button in my app. When it is pressed I call the onSearchRequested() method. When this is called the stock search box comes up so the user can type some junk in there. When they press "Go" I need the results returned back to my application but instead the stock browser is opened and a google search is performed. How can I tell it to return the results to my application instead.
I've tried these items in my manifest.xml file:
<meta-data android:name="android.app.default_searchable"
android:value=".MainActivity" />
<activity android:name=".MainActivity"
android:launchMode="singleTop"
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
I've also tried to capture the result in my onCreate with this:
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction()))

This is covered fairly well in the documentation. In particular, you are missing the searchable <meta-data> element (and, presumably, the corresponding XML file). Here is a sample project showing the use of search within an application.

Related

Change application's name without touching the default activity

I have an Android application with a default activity which is defined in the AndroidManifest.xml:
<application
android:label="NameOfMyApp"
...
<activity
android:name=".LoginActivity"
android:label="Login">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.rei0d.wop.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
It seems that android:label="NameOfMyApp" will be overwritten if there is an attribut android:label for the default activity.
So I want to change the application's name to something else than Login, NameOfMyApp for example, without changing the android:label of the Login activity. Is this possible? Or do I have to create a blanc activity as default activity which starts the Login activity?
Well, there are multiple possibilities to fix this issue, unfortunately some of them don’t work on all devices.
It seems like that the Launcher takes the application-name from the label of the "Entry Activity". You can prevent his by adding the following attribute to your intent-filer:
<intent-filter android:label="NameOfMyApp">
Unfortunately some Launchers may ignore this, so I wouldn’t go for this solution.
I would recommend you to change the title of your Login-Activity programatically. So first of all change the android:label value of your .LoginActivity to the name of your app.
In onCreate or somewhere else set the title (assuming that you’re using the ActionBar).
getActionBar().setTitle("Login");
Note: You should get the title from a strings-xml file of course

Perform search on Android within the same activity

I'm trying to integrate search in my app but I'm being blocked by one issue. My activity is declared like this and is located inside of TabActivity.
<activity
android:name="FileBrowserActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
As you can see its launchMode is set to singleTop. But when I invoke search, input the search query and press enter another instance of such activity is launched. No onNewIntent is being called! But in the launched activity each new search doesn't launch a new activity.
The desired behaviour is to stay within current activity. Basically that's how android launcher search works.
You should rather use android:launchMode="singleTop" like described in this article.
You could use android:noHistory http://developer.android.com/guide/topics/manifest/activity-element.html#nohist

Search suggestions causing inconsistent behavior in application after triggered from Quick Search Bar

I'm running into an issue with the Quick Search Bar that I'm hoping someone can help me with.
My application includes a Searchable activity used to provide search behavior within my application. That search activity is designed to trigger a separate Intent when the search item is clicked on so as to cause a different Activity to handle the viewing. When used within the application, the search behavior works perfectly every time.
My application also includes a ContentProvider used to provide search suggestions and is also configured to allow use in the Quick Search Bar. When used within the application itself, use of the search suggestions works fine every time it is used. When triggered from the QSB, the initial search suggestion brings up the viewing activity just as it should. After that point, however, any use of the search suggestions from within the application (i.e. bringing up the search and selecting a search suggestion) fails to trigger the viewing application. In fact, I've put debug statements in every single "onXXX()" method within the Searchable activity and I never see any of them get triggered. On the flip side, when I trigger a standard search at that same point (i.e. enter a query string and hit enter, rather than navigating to a search suggestion), the search dialog comes up, as expected, and selecting an item from that list successfully triggers my application.
I'm currently at a loss trying to determine why this would be occurring. Any ideas
As as some additional information, my manifest contains the following in regards to the searchable activity (".activity.SearchableActivity"), the suggestions provider (".content.TestSuggestionProvider") and the activity used to display the content (".activity.TestDisplayActivity"):
<activity
android:name=".activity.TestDisplayActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:finishOnTaskLaunch="true"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable" android:value=".activity.SearchableActivity" />
</activity>
<activity
android:name=".activity.SearchableActivity"
android:launchMode="singleTop"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable"/>
</activity>
<provider
android:name=".content.TestSuggestionProvider"
android:authorities="com.test.provider.suggest"
android:syncable="false"
/>
And the following is the XML used to further define the settings of the searchable activity:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/search_app_label"
android:hint="#string/search_app_hint"
android:searchSettingsDescription="#string/search_app_settings_description"
android:includeInGlobalSearch="true"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:searchSuggestAuthority="com.test.provider.suggest"
android:searchSuggestIntentAction="android.intent.action.VIEW">
</searchable>
Any thoughts? At the moment I'm at a complete loss…

Android: making search available throughout the whole application

Right now I'm adding
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable" />
to every single <activity> in the AndroidManifest.xml, receive the Intent in each activity and forward the search string to one singleTop activity that actually performs the search (via HTTP/JSON) and displays the results in a list.
I'm wondering if there's a way how I can set-up the AndroidManifest.xml, so that search is activated in each activity within my application, but the search string will get forwarded from Android's search box directly to my singleTop SearchResults_Activity instead of going the way through validating events in each activity and then forwarding the search string.
The answer to that is actually available in the SearchManager documentation, in the MetaData chapter.
Activities which are part of a
searchable application, but don't
implement search itself, require a bit
of "glue" in order to cause them to
invoke search using your searchable
activity as their primary context. If
this is not provided, then searches
from these activities will use the
system default search context.
<application>
<meta-data android:name="android.app.default_searchable"
android:value=".MySearchActivity" />
<!-- followed by activities, providers, etc... -->
</application>

Android: forward search queries to one single activity that handles search

I have an activity handling search (ACTIVITY_1), which works perfectly when I use the search (via SEARCH button on the phone) within/from this activity.
However, when I use search from another activity (ACTIVITY_2..x) by implementing onNewIntent and forward the query string to my Search_Activity.class (ACTIVITY_1)
#Override
protected void onNewIntent(Intent intent) {
Log.i(TAG, "onNewIntent()");
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
Log.i(TAG, "===== Intent: ACTION_SEARCH =====");
Intent myIntent = new Intent(getBaseContext(), Search_Activity.class);
myIntent.setAction(Intent.ACTION_SEARCH);
myIntent.putExtra(SearchManager.QUERY, intent.getStringExtra(SearchManager.QUERY));
startActivity(myIntent);
}
}
it always pauses ACTIVITY_2 first and then goes to onCreate() of ACTIVITY_2.
Why does it recreate my ACTIVITY_2 when it is already there and doesn't go to onNewIntent directly?
Is there another way I can forward search queries directly to ACTIVITY_1? For example via a setting in the Manifest.xml
Is it possible to generally forward all search queries automatically to ACTIVITY_1 without even implementing onNewIntent in all the other activities?
Currently I have to put an <intent-filter> in every single activity to "activate" my custom search there and forward the query then to the activity that handles search via the onNewIntent (as shown above).
<activity android:name=".Another_Activity"
android:theme="#style/MyTheme">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
I'm not sure I understand the chain of events your describing, but here's how you need to configure your application in the case were ACTIVITY_1 is the search Activity you always want to launch from all your other Activities when the user presses the 'search' button.
Assuming that the search button works perfectly on Activity1, you just need to add a bit of glue meta-data to your application telling it that all your other Activities should use ACTIVITY_1 for searching, as shown in the manifest snippet below:
<application>
<meta-data
android:name="android.app.default_searchable"
android:value=".ACTIVITY_1" />
<!-- All your activities, service, etc. -->
</application>
Using this, you should be able to remove the intent-filters from all but ACTIVITY_1, and you won't need to use the onNewIntent handler in any of your other Activities.

Categories

Resources