Toolbarに音声入力
色々ググったけど、マイクが表示されず、ハマりまくった。
何とか形になったのでメモ。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/ThemeOverlay.AppCompat.Dark" />
<TextView
android:id="@+id/result"
android:textSize="20sp"
android:layout_margin="10dp"
android:textColor="#FF34"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jp.co.araiyusuke.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- singltTop スタックのトップに同じアクティビティがある場合は新たにアクティビティを生成せずに使い回す -->
<activity
android:name=".MainActivity"
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.default_searchable"
android:value=".MainActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
</manifest>
res/xml/searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 末尾にvoiceSearchModeを追加 -->
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/hint"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" />
res/menu/menu_search.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search_view"
android:title="@string/app_name"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
strings.xml
<resources>
<string name="app_name">My Application</string>
<string name="hint">住所</string>
</resources>
MainActivity.java
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private SearchView mSearchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("デバッグ", "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setToolbar();//ツールバー初期化
}
private void setToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
mSearchView = (SearchView) menu.findItem(R.id.search_view).getActionView();
mSearchView.setSubmitButtonEnabled(false);
mSearchView.setMaxWidth(Integer.MAX_VALUE);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchView.findViewById(android.support.v7.appcompat.R.id.search_plate)
.setBackgroundColor(getResources().getColor(android.R.color.transparent));
mSearchView.findViewById(android.support.v7.appcompat.R.id.submit_area)
.setBackgroundColor(getResources().getColor(android.R.color.transparent));
mSearchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
//確定イベント
@Override
public boolean onQueryTextSubmit(String query) {
Log.d("onQueryTextSubmit", query);
return false;
}
//テキスト入力ごとに呼ばれるイベント
@Override
public boolean onQueryTextChange(String query) {
Log.d("onQueryTextChange", query);
return false;
}
});
//アクティビティ開始時から、カーソルを合わせる
boolean autofocus = false;
mSearchView.setIconified(!autofocus);
mSearchView.setIconifiedByDefault(true);
return true;
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Log.d("onNewIntent", query);
boolean submit = false;
mSearchView.setQuery(query, submit);
TextView mResultTv = (TextView) findViewById(R.id.result);
mResultTv.setText(query);
}
}
@Override
protected void onStart() {
super.onStart();
Log.d("デバッグ", "onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.d("デバッグ","onResume");
}
}