Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a custom SearchView layout?

I am trying to implement a custom SearchView layout. I have successfully changed many of the attributes of android.support.v7.appcompat.SearchView such as background color and text color, but I am wanting to customize even more. So I made my own search_view_layout.xml file, but I cannot figure out how to implement it. This is the search_view_layout.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_gravity="top"
              android:background="#FFFFFF"
              android:layout_width="match_parent"
              android:layout_height="40dp"
              android:paddingLeft="5dp"
              android:paddingTop="5dp"
              android:paddingRight="5dp"
              android:paddingBottom="5dp"
              android:padding="5dp">

    <SearchView
        android:id="@+id/leftMenuSearch"
        android:background="#FFFFFF"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:queryHint="What are you looking for?"
        android:layout_weight="1"
        android:layout_marginEnd="5dp"
        android:layout_marginRight="5dp"
        android:maxWidth="1000dp"/>

    <Button
        android:id="@+id/cancel_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:background="@color/beanBlue"
        android:text="Cancel"
        android:textColor="#FFFFFF"/>

</LinearLayout>

and this is my menu_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
      xmlns:appcompat="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_search"
          android:title="Search"
          android:icon="@drawable/search_icon"
          app:showAsAction="always|collapseActionView"
          app:actionLayout="@layout/search_view_layout"
          app:actionViewClass="android.support.v7.widget.SearchView"/>

    <item android:id="@+id/action_cart"
          android:title="Cart"
          android:icon="@drawable/cart_icon"
          app:showAsAction="ifRoom"/>
</menu>

and this is my toolbar.xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#000000"
    android:elevation="5dp"
    android:contentInsetLeft="15dp"
    app:collapseIcon="@drawable/collapse_back_button">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bean"
        android:layout_gravity="center"
        android:textColor="@color/beanBlue"
        android:id="@+id/toolbar_title"
        android:textSize="25sp"/>


</android.support.v7.widget.Toolbar>

This is what the toolbar looks like before the search icon is pressed:

Toolbar before Search

and this is what I want the SearchView to look like when the search icon is pressed:

Toolbar with SearchView

How do I implement this custom SearchView layout in my android project?

like image 539
Carson Avatar asked Mar 07 '16 20:03

Carson


People also ask

How do I customize SearchView?

just do your own search view, it is very simple. you then can include this layout in your activity layout file. This is a simple layout which includes a "search icon" followed by EditText, followed by "clear icon". The clear icon is shown after user types some text.

How do I create a search bar on Android?

Add the Search View to the App Bar To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.

How do I get text in SearchView?

To get text of SearchView , use searchView. getQuery() .

How do I use search view?

Android SearchView provides user interface to search query submitted over search provider. SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class.


1 Answers

final int layoutResId = a.getResourceId(R.styleable.SearchView_layout, R.layout.abc_search_view);
inflater.inflate(layoutResId, this, true);

I found the code above in android.support.v7.widget.SearchView.java, you can define your custom layout in xml, and just add the app:layout="@layout/resId".

Notice

You have to read the source code to provide all views with the same ids, or it will crash with NullPointerException. For detail, you can look at the source of SearchView(Context context, AttributeSet attrs, int defStyleAttr)

like image 113
Dennis Lee Avatar answered Oct 07 '22 15:10

Dennis Lee