Android Searchview and the magnifying icon

Just a quick note about the android.support.v7.widget.SearchView.

Normally the widget has the magnifying on the left side if expanded. So if you do something like this:

SearchView searchView =
   (SearchView) findViewById(R.id.search_view);
searchView.setIconifiedByDefault(false);
searchView.setIconified(false);

We will end up with something like this:

Android Searchview standard

If we need to manipulate the magnifying icon, we need a reference to that View:

ImageView searchViewIcon =
   (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);

After that just retrieve the parent ViewGroup. It is, at least today, a LinearLayout, so just remove the View and add it again to have it at the end / right side of the SearchView.

ViewGroup linearLayoutSearchView =
   (ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);
linearLayoutSearchView.addView(searchViewIcon);

So we have something like this:

Android Searchview right

Of course, it is a standard View, so if we need to remove it altogether, just set its properties accordingly (from here):

searchViewIcon.setAdjustViewBounds(true);
searchViewIcon.setMaxWidth(0);
searchViewIcon.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
searchViewIcon.setImageDrawable(null);