I am implementing Action Bar
. I want 2 buttons in Search field area of Action Bar SearchView
like Flipkart app.
Like above screenshot, I want 2 buttons, first one is for Voice Search and another is for Barcode Scanner, when Search icon clicked and SearchView
of Actionbar
appears. I am able to implement actionbar serahcview
with search icon. But I am bit confused why voice search not appearing in my app. Have a look on my source code.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
setSearchTextColour(searchView);
setCloseSearchIcon(searchView);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_search:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setSearchTextColour(SearchView searchView) {
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
searchPlate.setTextColor(getResources().getColor(R.color.novoda_blue));
// searchPlate.setBackgroundResource(R.drawable.edit_text_holo_light);
searchPlate.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
}
private void setCloseSearchIcon(SearchView searchView) {
try {
Field searchField = SearchView.class.getDeclaredField("mCloseButton");
searchField.setAccessible(true);
ImageView closeBtn = (ImageView) searchField.get(searchView);
closeBtn.setImageResource(R.drawable.action_cancel);
searchField = SearchView.class.getDeclaredField("mVoiceButton");
searchField.setAccessible(true);
ImageView voiceBtn = (ImageView) searchField.get(searchView);
voiceBtn.setImageResource(R.drawable.ic_launcher);
} catch (NoSuchFieldException e) {
Log.e("SearchView", e.getMessage(), e);
} catch (IllegalAccessException e) {
Log.e("SearchView", e.getMessage(), e);
}
}
Screenshot of What I got from above code :
Any help would be highly appreciated.
First, you should read this
AndroidManifest.xml for your activity with searchview widget (singleTop is necessary, please read the link above):
<activity android:name=".activities.MainActivity"
android:configChanges="orientation|screenSize"
android:launchMode="singleTop" >
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
Create .../res/xml/searchable.xml with this:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/hint"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>
Modify onCreateOptionsMenu (last two lines are matter)
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_action_bar_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint(getResources().getString(R.string.hint));
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
And finally override onNewIntent():
@Override
protected void onNewIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
After these changes you will see a mic button in a searchview.
Please check in your code if you didn't call:
searchView.setIconifiedByDefault(true)
By removing it you should see the 'voice' icon.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With