Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

searchView.setSubmitButtonEnabled(false);    
LinearLayout linearLayoutOfSearchView = (LinearLayout) searchView.getChildAt(0);
Button yourButton = new Button(mContext); // and do whatever to your button
linearLayoutOfSearchView.addView(yourButton);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top