Search code examples
androidandroid-custom-viewsearchview

How to create a simplified search interface?


I want to implement search on ListView which should look like this (Google Keep labeling interface):

  • no delete button
  • no search icon
  • no settings button
  • back button only

enter image description here enter image description here

Should I?:

  1. implement layout with EditText, attach it to Action Bar and remove anything else from ActionBar
  2. create custom SearchView (if so, how to get accesss to SearchView layout?)
  3. remove Action Bar somehow and make EditText in main layout

Solution

  • First make custom layout for action bar (only EditText for simplicity):

    custom_action_bar_layout.xml
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
    <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/editText" android:inputType="text"/>
    </LinearLayout>
    

    Add to Activity:

    // hide icon
    getActionBar().setDisplayShowHomeEnabled(false);
    // hide title
    getActionBar().setDisplayShowTitleEnabled(false);
    // set custom layout
    getActionBar().setCustomView(R.layout.custom_action_bar_layout);
    // enable custom view to be shown
    getActionBar().setDisplayShowCustomEnabled(true);
    // access to custon veiw element
    EditText text = (EditText) findViewById(R.id.editText);