Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Add a custom button to SearchView Widget in Actionbar

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?

enter image description here

like image 857
Sharj Avatar asked Mar 20 '14 07:03

Sharj


People also ask

How to set SearchView in Toolbar in android?

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.

How to Add Search Button in android Studio?

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.

How to implement SearchView in android Studio?

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.


2 Answers

searchView.setSubmitButtonEnabled(false);    
LinearLayout linearLayoutOfSearchView = (LinearLayout) searchView.getChildAt(0);
Button yourButton = new Button(mContext); // and do whatever to your button
linearLayoutOfSearchView.addView(yourButton);
like image 119
Lee Chun Hoe Avatar answered Sep 19 '22 23:09

Lee Chun Hoe


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.

like image 23
Sharj Avatar answered Sep 18 '22 23:09

Sharj