AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blogsearchable"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SearchActivity"
android:label="@string/title_activity_search" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<--IMPORTANTE AÑADIR ESTE META EN LA ACTIVITY QUE CONTIENE LA BARRA-->
<meta-data
android:name="com.example.blogsearchable.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
</manifest>
strings.xml
strings.xml
<resources>
<string name="app_name">BlogSearchable</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_search">BlogSearchable</string>
<string name="search">Search</string>
</resources>
res/xml/searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search"
android:label="@string/app_name" />
res/menu/options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
android:title="@string/search"
android:icon="@drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
SearchActivity.java
package com.example.blogsearchable;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;
public class SearchActivity extends Activity {
SearchView mSearchView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
}
@SuppressLint({ "NewApi", "NewApi" })
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem search = (MenuItem) menu.findItem(R.id.search);
mSearchView = (SearchView) search.getActionView();
mSearchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
setupSearchView(search);
return true;
}
@SuppressLint({ "NewApi", "NewApi" })
private void setupSearchView(MenuItem searchItem) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (searchManager != null) {
List<SearchableInfo> searchables = searchManager
.getSearchablesInGlobalSearch();
SearchableInfo info = searchManager
.getSearchableInfo(getComponentName());
for (SearchableInfo inf : searchables) {
if (inf.getSuggestAuthority() != null
&& inf.getSuggestAuthority().startsWith("applications")) {
info = inf;
}
}
mSearchView.setSearchableInfo(info);
}
}
}
Aquí les dejo una imagen de una búsqueda. Por cierto, notad que no se ha modificado ni el layout por defecto que viene de Android.
It works!
Roger Sala,
No hay comentarios:
Publicar un comentario