Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Creating a custom SearchView

I am having a List Activity which include a SearchView and ListView. I wish to have SearchView similar to what is present in google maps android app.

enter image description here

Currently my SearchView code in activity_main.xml is

<SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Search restaurants"
        android:iconifiedByDefault="false"
        android:id="@+id/searchView" />

Can someone guide me how to get a custom SearchView like above.

like image 643
shubendrak Avatar asked Sep 29 '22 03:09

shubendrak


1 Answers

You can add a android.support.v7.widget.Toolbar from AppCompat Library in your layout. It acts like a custom ActionBar that can be placed anywhere. Then you can add a menu toggle, and a android.support.v7.widget.SearchView which can collapse/expand. finally, add some actions from a menu.xml.

Setting a custom Toolbar as your ActionBar replacement:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.blah);

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);
}
like image 114
S.D. Avatar answered Oct 20 '22 09:10

S.D.