Skip to content Skip to sidebar Skip to footer

Custom Autocompletevview Like Facebook Comments Field In Android

I want to make custom AutoCompleteView like this.. It should be populated when my special character is added (like facebook.. When you type @B then all friends those name starts w

Solution 1:

You have to make custom autocompleteview by extending class.. and code mentioned in your question to be changed.

publicclassCustomAutoCompleteextendsAutoCompleteTextView {
privateStringprevious="";
privateStringseperator="@";
booleanisState=false;

publicCustomAutoComplete(final Context context, final AttributeSet attrs,
        finalint defStyle) {
    super(context, attrs, defStyle);
    this.setThreshold(1);

}

publicCustomAutoComplete(final Context context, final AttributeSet attrs) {
    super(context, attrs);
    this.setThreshold(1);
}

publicCustomAutoComplete(final Context context) {
    super(context);
    this.setThreshold(1);
}

/**
 * This method filters out the existing text till the separator and launched
 * the filtering process again
 */@OverrideprotectedvoidperformFiltering(final CharSequence text, finalint keyCode) {
    StringfilterText= text.toString().trim();
    Stringlastchar= filterText.substring(filterText.length() - 1,
            filterText.length());
    if (filterText.length() == 1) {
        if (lastchar.equals(seperator)) {
            isState = true;
        } else {
            isState = false;
        }
    }
    previous = filterText.substring(0,
            filterText.lastIndexOf(getSeperator()) + 1);

    filterText = filterText.substring(filterText
            .lastIndexOf(getSeperator()) + 1);

    if ((lastchar.equals(seperator)) || isState) {
        super.performFiltering(filterText, keyCode);

        isState = true;

    }
}

/**
 * After a selection, capture the new value and append to the existing text
 */@OverrideprotectedvoidreplaceText(final CharSequence text) {
    isState = false;
    super.replaceText(previous + text);// + getSeperator());

}

public String getSeperator() {
    return seperator;
}

publicvoidsetSeperator(final String seperator) {
    this.seperator = seperator;
}

}

Hope this will help you...

Solution 2:

You can achieve this using MultiAutoCompleteTextView. Just implement your own Tokenizer class and it works. For mentions I have written a class you can use it.

package howdysam.com.howdysuggesttext;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.MultiAutoCompleteTextView;


publicclassAtTokenizerimplementsMultiAutoCompleteTextView.Tokenizer {

    @OverridepublicintfindTokenStart(CharSequence text, int cursor) {
        inti= cursor;

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

        return i;
    }

    @OverridepublicintfindTokenEnd(CharSequence text, int cursor) {
        inti= cursor;
        intlen= text.length();

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

        return len;
    }

    @Overridepublic CharSequence terminateToken(CharSequence text) {
        inti= text.length();

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

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

Then on View section (Activity or Fragment) instead of doing

 edit.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

do following

edit.setTokenizer(newAtTokenizer());

It works.

Post a Comment for "Custom Autocompletevview Like Facebook Comments Field In Android"