I would like to add a custom button to SearchView widget.
Going through source code of search_view
layout file I found out that it has a LinearLayout
(submit_area
) that contains submit button and voice button.
So I added a dynamic button to submit_area. But it doesn't show my dynamically added button because if either submit button or voice button is not enabled then submit_area
is invisible. So it doesn't matter if I add anything dynamically.
SearchView searchView = (SearchView) menuItem.getActionView();
int
submit_areaId = searchView.getContext().getResources()
.getIdentifier("android:id/submit_area", null, null);
Button btnScan = new Button(getActivity());
btnScan.setText(getString(R.string.scan)); submitArea.addView(btnScan);
However if I just set submit button enabled then it shows submit button as well as my dynamic button, like in the attached photo.
I just want to show my dynamic button only. Any clue how to achieve that?
To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.
Invoking the search dialog For instance, you should add a Search button in your Options Menu or UI layout that calls onSearchRequested() . For consistency with the Android system and other apps, you should label your button with the Android Search icon that's available from the Action Bar Icon Pack.
SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class. For making search field visible, SearchView uses setIconifiedByDefault(false) method.
searchView.setSubmitButtonEnabled(false);
LinearLayout linearLayoutOfSearchView = (LinearLayout) searchView.getChildAt(0);
Button yourButton = new Button(mContext); // and do whatever to your button
linearLayoutOfSearchView.addView(yourButton);
Since a private SearchView
class method is setting submit_area
Visibility.Gone
so we can't do much about it.
A quick dirty trick is to set of the default buttons enabled and then set its width and height to 0. Setting Visibility
didn't work so have to set height/width.
I enabled search button and then set its height/width to zero.
int submit_areaId = searchView.getContext().getResources().getIdentifier("android:id/submit_area", null, null);
ImageView submitImage = (ImageView) searchView.findViewById(search_go_btnId);
searchView.setSubmitButtonEnabled(true);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, 0);
submitImage.setLayoutParams(layoutParams);
Now SearchView will show anything that you add in submit_area.
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