1

I want to create custom tokenizer for @ like as whatspp feature (when open group and write @ then open popup for list and user can select any. Also user can remove that string of @.

I have found twitter like search feature (example), but in this, when user can write @ then do not show popup window of list. User can write something after @ then based on typing, popup window will show search result.

I want to show something like this:

enter image description here

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
dipali
  • 10,966
  • 5
  • 25
  • 51

3 Answers3

0

Please see TokenAutoComplete, I hope it help

Sergey Nikitin
  • 807
  • 8
  • 23
0

I got solution for my question.

i have create own custom view for multiautocompletetextview and add performFiltering method for open popup after @sign.

public class KcsMultiAutoCompleteTextView extends MultiAutoCompleteTextView {
    public KcsMultiAutoCompleteTextView(Context context) {
        super(context);
    }

    public KcsMultiAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public KcsMultiAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void performFiltering(CharSequence text, int start, int end, int keyCode) {
        if (text.charAt(start) == '@') {
            start = start + 1;
        } else {
            text = text.subSequence(0, start);
            for (int i = start; i < end; i++) {
                text = text + "*";
            }
        }
        super.performFiltering(text, start, end, keyCode);
    }

}
dipali
  • 10,966
  • 5
  • 25
  • 51
  • Hi @dipali....can explain how we get list at runtime from API on every time when write @, using this custom class? – Mohammad Misbah Jan 06 '17 at 14:10
  • @MohammadMisbah just use this custom class and set textview then it will perform filter in custom class arraylist. – dipali Jan 09 '17 at 04:46
0

Today I was looking for this. I used android-multiautocomplete library. Check the demo given with the library. In short, you have to extend 2 classes from the library, a tokenFilter (describes your pattern, in this case, @ character) and a ViewBinder (to customize the suggestion rows, such as placing user image at certain position etc). So for me, this is the TokenFilter :

import androidx.annotation.NonNull;
import ridmik.one.entities.MucOptions;
import ridmik.one.ui.components.autocomplete.filter.HandleTokenFilter;

public class MucUserTokenFilter extends HandleTokenFilter<MucOptions.User> {

  public MucUserTokenFilter() {
    this('@');
  }
  public MucUserTokenFilter(char handleChar) {
    super(handleChar);
  }

  @Override
  protected boolean matchesConstraint(@NonNull MucOptions.User user, @NonNull CharSequence constraint) {
    return user.getComparableName().toLowerCase().contains(constraint.toString().toLowerCase());
  }

  @Override
  public @NonNull CharSequence toTokenString(@NonNull MucOptions.User user) {
    return handleChar +user.getComparableName(); // Add handler at the begining! this is the trick!
  }
}

And this is the ViewBinder:

public class MucUserViewBinder implements AutoCompleteViewBinder<MucOptions.User> {
  public static final String TAG = MucUserViewBinder.class.getSimpleName();
  @Override
  public long getItemId(@NonNull @NotNull MucOptions.User user) {
    try{
      if(user!=null) {
        return user.userId;  // ei line e crash kore :/
      }
    }catch (Exception x) {
      Timber.tag(TAG).e("Exception "+x.getMessage());
    }


    return 0;
  }

  @Override
  public int getItemLayoutId() {
    return R.layout.simple_autocomplete_row;
  }

  @NonNull
  @NotNull
  @Override
  public AutoCompleteViewHolder getViewHolder(@NonNull @NotNull View view) {
    return new MucUserViewHolder(view);
  }

  @Override
  public void bindData(@NonNull @NotNull AutoCompleteViewHolder viewHolder, @NonNull @NotNull MucOptions.User user,
                       @Nullable @org.jetbrains.annotations.Nullable CharSequence constraint) {
    MucUserViewHolder itemViewHolder = (MucUserViewHolder) viewHolder;
    itemViewHolder.textView.setText(user.getComparableName());
    String smallAvatar = user.image + "_sm.png";
    Timber.tag(TAG).e("user.image = "+smallAvatar);
    Picasso.get().load(smallAvatar)
        .placeholder(ContextCompat.getDrawable(itemViewHolder.rootView.getContext(),
            R.drawable.ic_avatar_placeholder)).into(itemViewHolder.avatar);
  }

  static class MucUserViewHolder extends AutoCompleteViewHolder {
    public final TextView textView;
    public final CircleImageView avatar;
    public final View rootView;

    protected MucUserViewHolder(@NonNull @NotNull View view) {
      super(view);
      this.rootView = view;
      this.textView = view.findViewById(R.id.textView);
      this.avatar = view.findViewById(R.id.avatar);
    }
  }
}

After that, you go to your activity / fragment, and do this:


private MultiAutoComplete multiAutoComplete = null; // A field in your Activity / Fragment class, initialize it to null.

void initAutoSuggestions() {
  
ArrayList<MucOptions.User> someList = getUsers(); // <--- suggestions dataset depending on your code.
        AutoCompleteTypeAdapter<MucOptions.User> nameTypeAdapter =
            AutoCompleteTypeAdapter.Build.from(new MucUserViewBinder(), new MucUserTokenFilter());


        nameTypeAdapter.setItems(someList);

        this.multiAutoComplete = new MultiAutoComplete.Builder()
            .tokenizer(new PrefixTokenizer('@'))
            .addTypeAdapter(nameTypeAdapter)
            // .delayer(constraint -> { return 10; }) // eta ki bujhi ni
            .build();
        
        this.multiAutoComplete.onViewAttached(binding.textinput); // binding.textinput is your multiAutoCompleteView 
}

Then call it inside onCreate (for Activity) or onViewCreated (for fragment). Finally, inside the onDestroy:

    @Override
    public void onDestroy() {
        super.onDestroy();
          if(multiAutoComplete!=null) {
            this.multiAutoComplete.onViewDetached(); // needed for @mention that thing
        }
}

You can also see the Demo inside the library to see other options.

Qazi Fahim Farhan
  • 2,066
  • 1
  • 14
  • 26