I am trying to implement the search widget in the current android apps, but just can't get it done and I've not been able to implement it. Below is my code
MainActivity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search_bar).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} else if (id == R.id.search_bar) {
//startActivity(new Intent(this, SearchResultActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
MenuItem
<item android:id="@+id/search_bar"
android:title="Search"
android:orderInCategory="100"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
SearchActivity
public class SearchResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_custom_text);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
}
}
}
Manifest
<activity android:name=".MainActivity"
android:enabled="true">
<!--<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>-->
<meta-data android:name="android.app.default_searchable"
android:value=".SearchResultActivity"/>
</activity>
<activity android:name=".SearchResultActivity"
android:enabled="true"
android:launchMode="singleTop"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!--<meta-data android:name="android.app.default_searchable"
android:value=".SearchResultActivity" />-->
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
This is what I'm thinking, but I've not yet tried it. I did implemented a floating activity in my project. From the look of what's below, I think it's a floating Activity right. Any help would be appreciated.