I use following code snippets to enable the search functionality to my toolbar in the HomeActivity. When I enter a query, the application should take me to the NearbySearchActivity, where I will see the results.
I can see the toolbar, click on the icon and type a query in the SearchView that appears. However when I click on the magnifying glass on my software keyboard, it dissapears and nothing happens.
HomeActivity:
public class HomeActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
Xml file of HomeActivity's navigation icon:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:title="Search"
android:icon="@drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
AndroidManifest:
<activity android:name=".NearbySearchActivity">
<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=".HomeActivity"
android:label="@string/home_title"
android:theme="@style/AppTheme.NoActionBar" >
<meta-data android:name="android.app.default_searchable"
android:value=".NearbySearchActivity" />
</activity>
Searchable xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="search"/>
NearbySearchActivity:
public class NearbySearchActivity extends AppCompatActivity {
private String query = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby_search);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
query = intent.getStringExtra(SearchManager.QUERY);
}
}
}
How can I submit the query I have entered and go to the NearbySearchActivity?