2

I'm try to understand from where there is space between the back arrow and the searchview component. I'm using the separate activity for searching with tooltip.

I tried to add padding 0 to the search view but without success.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/search_toolbar_color"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            android:padding="0dp"
            app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

    </android.support.design.widget.AppBarLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/search_results"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:visibility="gone" />

    </RelativeLayout>
</LinearLayout>

I created custom search view to meet my requirements, open keyboard while entering search activity, remove the search icon and change the color scheme

  @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.search_menu, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);

        SearchManager searchManager = (SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
        if (searchItem != null)
        {
            _searchView = (SearchView) searchItem.getActionView();
        }

        if (_searchView != null)
        {
            _searchView.setSearchableInfo(searchManager.getSearchableInfo(this.getComponentName()));
            _searchView.setQueryHint(getString(R.string.action_search));
            _searchView.setIconifiedByDefault(false);
            final SearchView finalSearchView = _searchView;
            _searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener()
            {
                @Override
                public void onFocusChange(View view, boolean hasFocus)
                {
                    if (hasFocus)
                    {
                        //                      showInputMethod(view);
                        finalSearchView.onActionViewExpanded();
                    }
                }
            });


            //remove the icon in searchView
            ImageView searchViewIcon = (ImageView) _searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
            ViewGroup linearLayoutSearchView = (ViewGroup) searchViewIcon.getParent();
            linearLayoutSearchView.removeView(searchViewIcon);

            _searchView.setOnQueryTextListener(this);
            _searchView.requestFocus();

        }
        return true;
    }

too big space

Update:

After inspecting the toolbar layout by using view hierarchy I come to realized there is some kind of appcompat textview between the back arrow image to the search view. Unfortunately I still can't manage to know how to remove it from there.

enter image description here

Anton Makov
  • 825
  • 2
  • 9
  • 25

2 Answers2

0

You can set those attributes in Toolbar to minimize the extra espace (can set negatives values for margin):

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    xmlns:app="schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/search_toolbar_color"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:contentInsetLeft="0dp"
    android:contentInsetStart="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetEnd="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp" />

Original answer: https://stackoverflow.com/a/32320632/8405762

Alternatively you can use the SearchBar component with negative margin:

<android.support.v7.widget.SearchView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingLeft="-16dp"
  android:paddingStart="-16dp"
  app:iconifiedByDefault="false"
  app:searchIcon="@null"
  app:queryHint="Search"
  app:theme="@style/ThemeOverlay.AppCompat.Dark" />

SearchBar documentation

GuilhermeFGL
  • 2,891
  • 3
  • 15
  • 33
  • Unfortenetly it didn't work, there is some kind of text view that is taking some space in the tooltip. You can see my update in my first post – Anton Makov Aug 08 '17 at 19:37
  • Try replace your `Toolbar` with `SearchView`, them use `paddingLeft` with negative value to push the `TextView` to the left – GuilhermeFGL Aug 08 '17 at 19:41
-1

I found a solution that worked for me, I added this line

_searchView.setMaxWidth(Integer.MAX_VALUE); 

in onCreateOptionsMenu after you inflate the menu layout.

you can read more here [link][1]

Also I removed the title from the actionbar by using

getSupportActionBar().setDisplayShowTitleEnabled(false);

(*) There is still a space between the arrow and the searchview but it much more smaller and I can live with that.

(**) GuilhermeFGL, unfortunately the solutions you provided didn't worked for me, maybe it's because I'm running nougat. But thx for your help.

Anton Makov
  • 825
  • 2
  • 9
  • 25