Creating a Search Interface in a Android with Kotlin - android

I am having a hard time figuring out the documentation on creating a search Interface.
My main activity contains the search widget on the app bar but when I perform a search query it does not launch the Searchable Activity; responsible for displaying the results.
Below is my manifest file for the SearchbaleActivity and MainActivity
<activity
android:name=".activities.SearchableActivity"
android:exported="false"
android:label="#string/title_activity_searchable"
android:theme="#style/Theme.MoveApplication.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<activity
android:name=".activities.MainActivity"
android:exported="false" >
</activity>
Should my intent-filter and meta-data attributes be in my MainActivity or SearchableActivity?
And even so, my SearchableActivity should launch despite not handling the query, right?
My Android skills are not that great so i'll appreciate a vivid explanation with examples

Related

Android Search Activity and android:launchMode="singleTop"

I have a single activity in my app that uses a SearchView to search (see manifest-- the search intent is intercepted).
However, when I press search, nothing happens. The activity never re-starts itself. So, I removed android:launchMode="singleTop" from my activity and then it started re-starting itself and "working." However, it is preferable to have singleTop for me so that the activity only has one instance. Why is this not working when I have singleTop?
<activity
android:name="com.brianco.andypedia.MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable" />
</activity>
<meta-data android:name="android.app.default_searchable"
android:value=".MainActivity" />
I don't know if this answers your question, but this page shows a simple searchview example with ActionBar Compat (it is the new google library compatible for the old Android versions)
You need to process your search intent on onNewIntent besides onCreate.

How to make my app accept search intents from other apps?

My searchable activity currently looks like this in the Android Manifest:
<activity
android:name=".activity.Search"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
I would like to be able to add my application to the preinstalled "Google Search"-app as a searchable element and therefore accept its search intents.
Seems correct. Did you enable your app in the Google-Search app settings, to be searchable?
I just found this page on the android dev blog. What I've been missing was the xml attribute android:includeInGlobalSearch="true" in my searchable.xml.

Search Interface opens a new Activity when search

I am trying to use the Search Manager from Android.
In fact, I have an activity where I am calling
onSearchRequested()
Then, I am using in the same activity this function to get the search string:
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
}
The problem is the next: When i am clicking to the search button, I am opening a new activity and i would like to stay on the same and do some searches. So, my goal is to avoid the new activity opening when I click to the search button.
Thank you.
The solution consists to reuse the developpers example http://developer.android.com/guide/topics/search/search-dialog.html
The important thing is to create 2 activities, one for searches and one other for displaying the results.
In the manifest, use this code:
<application ... >
<!-- this is the searchable activity; it performs searches -->
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<!-- this activity enables the search dialog to initiate searches
in the SearchableActivity -->
<activity android:name=".OtherActivity" ... >
<!-- enable the search dialog to send searches to SearchableActivity -->
<meta-data android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
</activity>
...
use in Manifest
<activity android:name="BrowseItems" android:label="#string/browseitems"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/itemsearchable" />
</activity>

Android Manifest File Not Picking Up Default Launch Activity

If on run/debug configuration, if I specify a launch activity, then my app runs fine, but when I select 'Launch Default Activity' I get "No Launcher activity found!" error from android adb despite the fact that I have specified the launch activity in my manifest file. I'm using android 2.1.
I've tried refreshing the file/project, clean and rebuild, deleting the file and re-writing it, creating a new project and copy/pasting all the code across and doing Android Tools -> Fix Project Properties. I've tried Android Tools-> Rename Application Package, which prompts me to update the project launch configuration (to which I say yes, but it still doesn't fix the problem).
I've also tried different devices. My Home activity extends another activity and is in its own Home.java file in my src folder. This is what my manifest file looks like:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.geogreenweb.localvore"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="#drawable/icon"
android:label="#string/general_app_name"
android:debuggable="true">
<meta-data android:name="android.app.default_searchable"
android:value=".localvorebeta6" />
<activity android:name="com.geogreenweb.localvore.Home"
android:label="#string/general_app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<activity android:name="com.geogreenweb.localvore.Quit"
android:label="Quit">
</activity>
<activity android:name="com.geogreenweb.localvore.InSeason"
android:label="In Season">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<activity android:name="com.geogreenweb.localvore.List"
android:label="A - Z">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<activity android:name="com.geogreenweb.localvore.Local"
android:label="Local"
android:configChanges="orientation"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<activity android:name="com.geogreenweb.localvore.Details"
android:label="Details">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<activity android:name="com.geogreenweb.localvore.Search"
android:label="Search">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<activity android:name="com.geogreenweb.localvore.Map"
android:label="Map"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<activity android:name="com.geogreenweb.localvore.Help"
android:label="Help">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="7" />
</manifest>
UPDATE: I changed one of the activity labels, and the different label appears in the app, so the changes to the manifest file are being picked up, but still I get the No Launch Activity error
UPDATE 2: I managed to solve the problem by creating a new project in the end, but only if I select the option to create an activity when making the new project, then copy/paste in the rest of the stuff into the project
if you remove the :
<action android:name="android.intent.action.SEARCH" />
it will launch normally.
i don't know why .. it just worked for me. i'm still searching why?!!
UPDATE 1:
i found this:
No Launcher activity found, despite being declared in manifest.xml
still don't know why? .. still searching for it.
UPDATE 2: GOT IT!
http://developer.android.com/guide/topics/intents/intents-filters.html
A filter has fields that parallel the action, data, and category fields of an Intent object. An implicit intent is tested against the filter in all three areas. To be delivered to the component that owns the filter, it must pass all three tests. If it fails even one of them, the Android system won't deliver it to the component — at least not on the basis of that filter. However, since a component can have multiple intent filters, an intent that does not pass through one of a component's filters might make it through on another.
in our case this means that the system sends an intent with the MAIN action and LAUNCH category. it finds an with the following:
MAIN action ..............[ pass ]
LAUNCH category .... [ pass ]
SEARCH action .........[ fail ]
but .. if you put the SEARCH action in another , the containing the MAIN and LAUNCHER criteria passes.
The only thing I can think of is the lines
<activity android:name="com.geogreenweb.localvore.Home"
Try using just
<activity android:name=".Home"
instead
Try adding the following to your main launcher Activity right next to the launcher category element.
<category android:name="android.intent.category.DEFAULT"/>

How to declare default searchable activity

I am trying to implement search in my application.
My application contain 4 activities and I want to add the search dialog only on 3 of them while only one of them (ProductsActivity) will be the default context.
unfortunately while I activate the search I keep getting the following error:
"Key android.app.default_searchable expected String but value was a java.lang.Integer. The default value was returned."
<activity android:label="#string/app_name" class=".AppEntry" android:name=".AppEntry">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".category.CategoriesListActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable"/>
</activity>
<activity android:name=".product.ProductsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.default_searchable" android:resource="#xml/searchable"/>
</activity>
Any idea why ?
Thanks
For default searchable activity you have to put the meta-data tag under the application tag.
<application ... >
<meta-data android:name="android.app.default_searchable"
android:value=".DefaultSearchActivity"/>
<activity android:name=".ProductActivity" >
...
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivityForProducts"/>
</activity>
...
In that example the default application search will be done on the DefaultSearchActivity, while in the ProductActivity the search will be on SearchActivityForProducts. Hope it helps someone.
Shouldn't it be
<meta-data android:name="android.app.default_searchable"
android:value=".product.ProductsActivity"/>
instead of passing the #xml reference there again.
One thing really important here is to correctly name your activities as the Android guidelines explains http://developer.android.com/guide/topics/manifest/activity-element.html#nm
android:name The name of the class that implements the activity, a
subclass of Activity. The attribute value should be a fully qualified
class name (such as, "com.example.project.ExtracurricularActivity").
However, as a shorthand, if the first character of the name is a
period (for example, ".ExtracurricularActivity"), it is appended to
the package name specified in the element. Once you publish
your application, you should not change this name (unless you've set
android:exported="false").
There is no default. The name must be specified.
If you don't put the DOT in your activity name, your search action will only work on the activity you declared as "default_searchable". This little DOT cost us hours so be careful!

Categories

Resources