How to show MultiAutoCompleteTextView dropdown at some other points in the text

61 Views Asked by At

I've implemented the MultiAutoCompleteTextView and everything is working fine but now I want to suggestions at any point the user press the first character of the suggested words. Basically the suggestions appears at the start of the text and when the last character in the text is ","

So, this is my words list:

{"/Bag","/Carton","/Kg","/20cm"}

And I want suggestions to appear if the user press something like: 567/

I've tried overriding the enoughFilter() in my custom class like this:

public static Tokenizer mTokenizer = new MultiAutoCompleteTextView.CommaTokenizer();

@Override
    public boolean enoughToFilter() {
        Editable text = getText();
        
        int end = getSelectionEnd();
        if (end < 0 || mTokenizer == null) {
            return false;
        }
        int start = mTokenizer.findTokenStart(text, end);
        if (end - start >= getThreshold() || text.charAt(text.length()-1) == '/') {
            return true;
        } else {
            return false;
        }
    }

But this doesn't work at all and it crashes my code. Anyone with the idea?

1

There are 1 best solutions below

0
On

And finally I was able to achieve this by overriding enoughFilter(), performFiltering() and replaceText() in my custom class.

So, for anyone who wants to implement something similar, check my code below:

public class MyAutoCompleteEdittext extends MultiAutoCompleteTextView
{
    public static Tokenizer mTokenizer = new MultiAutoCompleteTextView.CommaTokenizer();
    
    public MyAutoCompleteEdittext(Context context){
        super(context);
    }
    public MyAutoCompleteEdittext(Context context, AttributeSet attrs){
        super(context,attrs);
    }
    public MyAutoCompleteEdittext(Context context, AttributeSet attrs,int defStyle ){
        super(context,attrs,defStyle);
    }
    
    @Override
    public boolean enoughToFilter() {
        String subText = getText().toString().substring(0,getSelectionEnd());
        if(subText.contains("/"))
            return true;
        return false;
    }
    
    @Override
    protected void performFiltering(CharSequence text, int start, int end, int keyCode)
    {
        super.performFiltering(text,findTokenStart(text.toString(),end),end,0);
    }
    
    @Override
    protected void replaceText(CharSequence text) {
        clearComposingText();
        Editable editable = getText();
        int end = getSelectionEnd();
        int start = findTokenStart(editable.toString(),end);
        String original = TextUtils.substring(editable, start, end);
        QwertyKeyListener.markAsReplaced(editable, start, end, original);
        editable.replace(start, end, mTokenizer.terminateToken(text));
    }
    
    private int findTokenStart(String allText, int end){
        return allText.substring(0,end).lastIndexOf("/");
    }
    
}

If you want more customization, check the source code HERE. That helps me a lot.