-1

How to implement @callout in android with AutoCompleteTextView.I want to show suggestion list when i type @ in textbox like facebook.

MohsinSyd
  • 175
  • 2
  • 13

1 Answers1

0

Just add a TextWatcher with AutoCompleteTextView.addTextChangedListener and implement the afterTextChanged to fetch for the @, if you find it just update the adapter of the autocompelete.

Sample:

final AutoCompleteTextView tx = (AutoCompleteTextView)findViewById(R.id.text);
    tx.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // get previous "@" or space
            String sub = s.toString().substring(0, start + count);

            if(sub.isEmpty())
                return;

            int index = -1;

            for( int i = start + count - 1; i >= 0; --i){
                if(sub.charAt(i) == '@') {
                    index = i;
                    break;
                }else if(sub.charAt(i) == ' '){
                    break;
                }
            }

            // No @ found before a space or the start of the string
            if(index == -1)
                return;

            String valueToSearch = s.toString().substring(index + 1, start + count);

            tx.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, /*filtered results with valueToSearch*/));
            tx.showDropDown();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
user3864005
  • 101
  • 3
  • @msn see the new sample I wrote, it might not contemplate all the cases but I think it will do the basic feature you need – user3864005 Nov 27 '14 at 22:02