Search code examples
androidsearchview

How add space (e.g.height = 20dp) between searchView and dropdown list?


In my fragment (not in ToolBar) I has SearchView component. Here fragment's layout in xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.SearchView
        android:id="@+id/searchView"
        style="@style/Widget.AppCompat.SearchView.ActionBar"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:background="@drawable/search_view_bg"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:searchHintIcon="@null" />    
</android.support.constraint.ConstraintLayout>

Here my dropdown's item layout in xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="@dimen/standard_min_height_container">

    <ImageView
        android:id="@+id/catalogImageView"
        android:layout_width="28dp"
        android:layout_height="28dp"
        android:src="@drawable/suggestion_icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_goneMarginStart="@dimen/standard_margin" />

    <TextView
        android:id="@+id/catalogNameTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:text="Test text View Test text "
        android:textColor="@android:color/black"
        android:textSize="15sp"
        app:layout_constraintBottom_toBottomOf="@+id/catalogImageView"
        app:layout_constraintLeft_toRightOf="@+id/catalogImageView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/catalogImageView" />

    <View
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="@color/divider_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="@+id/catalogImageView"
        app:layout_constraintRight_toRightOf="@+id/catalogNameTextView" />    
</android.support.constraint.ConstraintLayout>

Here java code in my fragment:

searchView = view.findViewById(R.id.searchView);
SearchManager sMng = context.getSystemService(Context.SEARCH_SERVICE);    
searchView.setSearchableInfo(sMng.getSearchableInfo(getComponentName()));
searchView.setSuggestionsAdapter(new SearchCatalogSuggestionsAdapter(ctx);

And here result:

SearchView_and_dropdown

But I need to add space (with height = 20dp) between searchView and dropdown list? How I can do this?


Solution

  • Please use this method

    private void setDropdownWidthHeight() {
     SearchView.SearchAutoComplete mSearchSrcTextView = (SearchView.SearchAutoComplete) 
     searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
     mSearchSrcTextView.setDropDownVerticalOffset(100);  //height in pixels
    }