3

What i want to do

i want to mention both people and group in a chat activity.when the user types @ i want populate the list which contains no of user and channels so that he can mention both people and channels

What i have done

i took help from this question Android: Autocomplete TextView Similar To The Facebook App here i have included my code

1.Arraylist which has users

   ArrayList<People> users = new ArrayList<User>();
        for (People user : SocketSingleton.userMap.values()) {
            if (user.getId() != loggedUserId) {
                users.add(user);
            }
        }

2.Arraylist which has groups list

ArrayList<Group> groups = new ArrayList<Group>();
        for (Group channel : SocketSingleton.listgroups.values()) {
            groups.add(channel);
        }

3.Adapters for view

final UserAdapter Adapter = new UserAdapter(getActivity(), R.layout.all_user_list_item, users);

final GroupAdapter Adapter1 = new GroupAdapter(getActivity(), R.layout.all_groups_list_item, Groups);

4.MultiAutocompletetextview

textinput = (MultiAutoCompleteTextView) view.findViewById(R.id.message_input);
        textinput.setAdapter(Adapter);
        textinput.setThreshold(0);

        textinput.setTokenizer(new MultiAutoCompleteTextView.Tokenizer() {

            @Override
            public CharSequence terminateToken(CharSequence text) {
                int i = text.length();

                while (i > 0 && text.charAt(i - 1) == ' ') {
                    i--;
                }

                if (i > 0 && text.charAt(i - 1) == ' ') {
                    return text;
                } else {
                    if (text instanceof Spanned) {
                        SpannableString sp = new SpannableString(text + " ");
                        TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0);
                        return sp;
                    } else {
                        return text + " ";
                    }
                }
            }

            @Override
            public int findTokenStart(CharSequence text, int cursor) {
                int i = cursor;

                while (i > 0 && text.charAt(i - 1) != '@') {
                    i--;
                }

                //Check if token really started with @, else we don't have a valid token
                if (i < 1 || text.charAt(i - 1) != '@') {
                    return cursor;
                }

                return i;
            }

            @Override
            public int findTokenEnd(CharSequence text, int cursor) {
                int i = cursor;
                int len = text.length();

                while (i < len) {
                    if (text.charAt(i) == ' ') {
                        return i;
                    } else {
                        i++;
                    }
                }

                return len;
            }
        });
        textinput.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Layout layout = textinput.getLayout();
                int pos = textinput.getSelectionStart();
                int line = layout.getLineForOffset(pos);
                int baseline = layout.getLineBaseline(line);

                int bottom = textinput.getHeight();

                textinput.setDropDownVerticalOffset(baseline - bottom);

            }

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

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        textinput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int id, KeyEvent event) {
                if (id == R.id.send || id == EditorInfo.IME_NULL) {
                    messageSend();
                    return true;
                }
                return false;
            }
        });

Problem i have

with this code i can able to mention only people. i can only set either adapter1 or adapter2 for Autocomplete tectview. So it is only displaying either users or groups. i want to display all list in single adapter so that the user can mention peoples and groups i am new to this please help me to find a way

Community
  • 1
  • 1
Sabarinathan
  • 439
  • 1
  • 7
  • 19

1 Answers1

0

There is no built in support for multiple data types with MultiAutoCompleteTextView. However there are several open source libraries that allow you to do just that. I've had the same requirement in the project I'm working on and here is our open source solution, feel free to check it out: https://github.com/Teamwork/android-multiautocomplete

You can achieve that by doing this:

MultiAutoComplete autoComplete = new MultiAutoComplete.Builder()
    ...
    .addTypeAdapter(typeAdapter1)
    .addTypeAdapter(typeAdapter2)
    .build();

You can also specify the type of filter and handle ('@' or '#' or any other) you want to use for each type adapter.

fast3r
  • 1,298
  • 13
  • 15