I realized SearchView
like Google says:
Created searchable.xml
in res/xml
:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="Search"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" />
Created menu item in res/menu/second_activity_menu
:
<item
android:id="@+id/second_toolbar_search"
android:icon="@mipmap/ic_search_black_24dp"
android:orderInCategory="100"
android:title="@string/search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
Change my AndroidManifest
:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
And realized my SecondActivity
class which extends AppCompatActivity
:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.second_activity_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.second_toolbar_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
As a result I have this:
After clicling on the search icon I have this:
When I start typing something, it doesn't show and it doesn't show "clear" icon and mic icon either. But I added them in searchable.xml
.
Thanks for any ideas or code.
ADD: maybe the reason is with toolbar style?
ADD 2: toolbar, but I don't think that it is nessesary
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
local:theme="@style/AppTheme" />