0

I want to change the background of the SearchView in the Appbar which is available default in Android using Java.

The below image shows how my current app bar looks like:

enter image description here

The below image shows what kind of SearchView background I want to achieve (Something similar to this):

enter image description here

The below is the code that I have for the search bar:

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) item.getActionView();

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }
    @Override
    public boolean onQueryTextChange(String newText) {
       adapter.getFilter().filter(newText);
       adapter.getFilter2().filter(newText);
       return false;
    }
});

The xml code is:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="#1F2D78"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:titleTextColor="@color/white">

Please help me to figure out how to achieve this

  • Unfortunately you can't change background of SearchBar. You can use custom background layout in toolbar. – Jitendra Singh May 22 '21 at 03:07
  • Does this help you? [Theme for SearchView in ActionBar](https://stackoverflow.com/questions/29145956/theme-for-searchview-in-actionbar) – sozsoy May 22 '21 at 13:23

1 Answers1

0

Instead of using menu search view you can use custom edittext inside toolbar.

        <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        app:elevation="0dp">

        <com.google.android.material.appbar.MaterialToolbar
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@color/white"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:gravity="center">

                <ImageView
                    android:id="@+id/btnBack"
                    android:layout_width="22dp"
                    android:layout_height="match_parent"
                    android:layout_marginEnd="10dp"
                    android:layout_marginStart="15dp"
                    android:src="@drawable/ic_back" />

                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/searchContainer"
                    android:layout_weight="1"
                    app:endIconMode="none"
                    android:layout_width="0dp">

                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/edtSearch"
                        android:drawableStart="@drawable/ic_search"
                        android:drawablePadding="3dp"
                        android:hint="Search.."
                        android:paddingStart="7dp"
                        android:paddingEnd="7dp"
                        android:inputType="textPersonName"/>
                </com.google.android.material.textfield.TextInputLayout>
            </LinearLayout>
        </com.google.android.material.appbar.MaterialToolbar>
    </com.google.android.material.appbar.AppBarLayout>

and you can customize your TextInputLayout

Enes Kayıklık
  • 351
  • 5
  • 12