Hick
Hick

Reputation: 36414

Making custom search suggestions in ActionBarSherlock search bar

This is how I do my basic search using the ActionBar Search widget. This is obviously the easy way where the suggestions are provided in a listView in the layout. But I want the suggestions inside the search box itself. Though it was possible to do it in a normal search box, how do I do the same using Actionbar Search box.

 public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        menu.add(0, 1, 1,"Search").setIcon(R.drawable.ic_search_inverse).setActionView(R.layout.collapsible_edittext).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        return super.onCreateOptionsMenu(menu);
     }



    @Override
        public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
            switch (item.getItemId()) {
                case 1:
                    search = (AutoCompleteTextView) item.getActionView();
                    search.addTextChangedListener(filterTextWatcher);
                    search.requestFocus();
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                    return true;
            }   
            return false;
        }       

    private TextWatcher filterTextWatcher = new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // your search logic here
            doGeoSearch(String.valueOf(s));



        }

    };

    public void doGeoSearch(String query){
        Geocoder geocoder;
        ArrayList<Address> addresses;
        ArrayList<String> address = new ArrayList<String>() ;
        geocoder = new Geocoder(this, Locale.getDefault());
        try {
            addresses = (ArrayList<Address>) geocoder.getFromLocationName(query, 6);
            Log.d("Address",String.valueOf(addresses));
            for(int i = 0;i<addresses.size();i++)
            {
            String addr = new String();
            addr.concat(addresses.get(i).getAddressLine(0));
            addr.concat(addresses.get(i).getAddressLine(1));
            addr = addresses.get(i).getAddressLine(0) + addresses.get(i).getLocality() + addresses.get(i).getAdminArea();
            //addr.concat(addresses.get(i).getAddressLine(2));
            Log.d("addr",addr);
            address.add(addr);


            }

            SearchAddressAdapater addressList = new SearchAddressAdapater(getApplicationContext(),R.layout.search_list,addresses, LocationActivity.this);
            //addressView.setAdapter(addressList);
            //ListView addressListView = new ListView();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

Upvotes: 0

Views: 2361

Answers (2)

Mridang Agarwalla
Mridang Agarwalla

Reputation: 45108

Even though the SearchView was implemented and works, the search suggestions work on newer devices, but don't work on older devices like Gingerbread. Here's an issue:

https://github.com/JakeWharton/ActionBarSherlock/issues/659

Upvotes: 0

Hip Hip Array
Hip Hip Array

Reputation: 4773

You can add a search widget to your ActionBar Sherlock, the search dialog has this functionality and it is very simple to implement as it is a simple expandable action item.

This tutorial will show you how to do everything you need with the search widget including search suggestions

Upvotes: 1

Related Questions