Search code examples
androidautocompleteandroid-custom-viewautocompletetextview

Custom AutoCompletevView like facebook comments field in android


I want to make custom AutoCompleteView like this..

enter image description here

It should be populated when my special character is added (like facebook.. When you type @B then all friends those name starts with 'B' will be populated and we can select name).

It should not be populate while typing until '@' is added.

When '@' is added autocompleteview is dropped down and we can select name and after selecting it will be appended.

enter image description here

I found this but not satisfied. I need clue. They have implemented...like when you type '@' dropdown populates.But I have to customize more. Right now I need help if other is having idea or any in-completed source.

I have to try myself but let me ask before implementing customview to save my time.


Solution

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

    public class CustomAutoComplete extends AutoCompleteTextView {
    private String previous = "";
    private String seperator = "@";
    boolean isState = false;
    
    public CustomAutoComplete(final Context context, final AttributeSet attrs,
            final int defStyle) {
        super(context, attrs, defStyle);
        this.setThreshold(1);
    
    }
    
    public CustomAutoComplete(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        this.setThreshold(1);
    }
    
    public CustomAutoComplete(final Context context) {
        super(context);
        this.setThreshold(1);
    }
    
    /**
     * This method filters out the existing text till the separator and launched
     * the filtering process again
     */
    @Override
    protected void performFiltering(final CharSequence text, final int keyCode) {
        String filterText = text.toString().trim();
        String lastchar = 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
     */
    @Override
    protected void replaceText(final CharSequence text) {
        isState = false;
        super.replaceText(previous + text);// + getSeperator());
    
    }
    
    public String getSeperator() {
        return seperator;
    }
    
    public void setSeperator(final String seperator) {
        this.seperator = seperator;
    }
    
    }
    

    Hope this will help you...