13

I'm trying to implement android.support.v7.widget.SearchView with a collapseIcon on the Toolbar. Everything works fine i.e, the SearchView is working as expected but I'm unable to remove/ reduce the padding between the SearchView and collapseIcon, as visible in the attached screenshot.

Any ideas on how can I do that?

SearchView

I've tried :

app:contentInsetStartWithNavigation="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"

but this doesn't work.

Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
  • 1
    Where is the `collapseIcon`? – MarkyDD Jan 09 '17 at 21:58
  • http://stackoverflow.com/questions/26455027/android-api-21-toolbar-padding – Mangesh Jan 10 '17 at 06:19
  • not relevant, I've already mentioned in the question that I've tried all that. Anyways, solved the issue. – Ashish Ranjan Jan 10 '17 at 06:41
  • 1
    @AshishRanjan how did you solve this (the leftMargin approach mentioned below does not work)? – Bootstrapper May 18 '18 at 10:00
  • did you try setting the insets mentioned in the question to zero? @Bootstrapper , if yes then something may have changed in the SearchView xml, you can try looking at the xml yourself. or maybe you did something wrong while implementing the solution. – Ashish Ranjan May 18 '18 at 17:37

2 Answers2

13

The problem can be solved by setting the leftMargin = 0 in the layout parameters associated with the following LinearLayout in the SearchView:

(To get the layout parameters use the getLayoutParams)

LinearLayout searchEditFrame = (LinearLayout) searchView.findViewById(R.id.search_edit_frame); // Get the Linear Layout 
// Get the associated LayoutParams and set leftMargin
((LinearLayout.LayoutParams) searchEditFrame.getLayoutParams()).leftMargin = 0;
Abhishek Kedia
  • 867
  • 6
  • 17
  • 1
    And in case anyone stumbles onto this looking to edit other properties, the XML can be found at https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/layout/search_view.xml – Abandoned Cart Jun 30 '19 at 14:52
10

I managed to solve the problem by looking at the xml file that's used by SearchView, so the padding can be removed/reduced by changing the leftMargin value:

layoutParams.leftMargin = 0;

for the following LinearLayout inside the SearchView:

LinearLayout searchEditFrame = (LinearLayout) searchView.findViewById(R.id.search_edit_frame);
Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39