Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Action Bar with Search View

I want to do an action bar that looks like this:

ActionBar

and I am not able to do this.

I tried to use that collapse thing described here, but it is not exactly what i want.

  • First of all SearchView isn't visible till user press search icon.. but I want It to be visible immediately after activity starts.
  • Secondly after SearchView appears all other action icons disappear.. But I want them to be visible all the time.
  • And it works only for android 3 and higher, I want it for android 2 and higher, so I am using ActionBarSherlock.

For example action bar in google maps looks similar to what I want.

Can somebody advise me how to achieve this?

like image 601
Vojtěch Bartoš Avatar asked Jul 09 '12 13:07

Vojtěch Bartoš


People also ask

How do I customize my SearchView widget?

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.


1 Answers

ActionbarSherlock is a great library. The latest(4.2.0) release has the method to make you search view visible when the activity starts SearchView.setIconified(boolean);

So if you want to make your search view always visible you should use the following code:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    //Create the search view
    SearchView searchView = new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext());
    searchView.setQueryHint("Search for countries…");
    searchView.setIconified(false);

    menu.add("Search")
            .setIcon(R.drawable.abs__ic_search)
            .setActionView(searchView)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);


}
like image 117
k_shil Avatar answered Oct 05 '22 22:10

k_shil