Home > Net >  Auto suggestion on editText for Android
Auto suggestion on editText for Android

Time:07-01

For my app, I need to add a suggestion feature when typing, I have used AppCompatAutoCompleteTextView and it works fine but I am struggling on 3 things:

When the user is typing, is there is no match in the suggestion, I would like a default text to be suggested. for example, you type jkdjfdkl and it shows automatically Other

    <string-array name="feedback_topic_array">
        <item>App Features</item>
        <item>App Experience</item>
        <item>Other</item>
    </string-array>

the other point is, when the user tap App it will suggest the 2 first item and it's fine but I would like to be able to set the word App in the suggested item in bold.

I have implemented this code:

binding.topicEt.apply {
            threshold = 0

           val suggestAdapter = ArrayAdapter(context, android.R.layout.simple_dropdown_item_1line, resources.getStringArray(R.array.feedback_topic_array) )
            setAdapter(suggestAdapter)
            showDropDown()
          //  performCompletion()
        }

but it's not setting the default value when no match

Any idea ?

CodePudding user response:

Use listener

Note: set at-least threshold = 1

binding.topicEt.validator = object: AutoCompleteTextView.Validator {
        override fun isValid(p0: CharSequence?): Boolean {
            //returns true or false based on if the text is validated
            return p0 == "COMPARE TEXT VALID";
        }

        override fun fixText(p0: CharSequence?): CharSequence{
            // isValid() returns false then the code comes here
            return "WHAT YOU WANT"
        }
    }

CodePudding user response:

This can be done with a custom adapter.

SimpleContainsAutocompleteAdapter autocompleteAdapter = new SimpleContainsAutocompleteAdapter(this,list);
autoCompleteTextView.setAdapter(autocompleteAdapter);
autoCompleteTextView.setThreshold(1);

If you found 0 results in the publishResults(), you can add your custom suggestion or you can say default suggestion.

Custom Adapter Class

class SimpleContainsAutocompleteAdapter<T> extends ArrayAdapter<T> implements Filterable {
        private List<T> listObjects;
        List<T> suggestions = new ArrayList<>();
        private int resource;

        private Filter mFilter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();

                if (constraint != null) {
                    suggestions.clear();
                    for (T object : listObjects) {
                        if (object.toString().toLowerCase().contains(constraint.toString().toLowerCase())) {
                            suggestions.add(object);
                        }
                    }

                    filterResults.values = suggestions;
                    filterResults.count = suggestions.size();
                }

                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence contraint, FilterResults results) {
                if (results == null) {
                    return;
                }
                List<T> filteredList = (List<T>) results.values;
                if(filteredList==null)
                {
                    clear();
                    notifyDataSetChanged();
                    return;
                }
                if (results.count > 0) {
                    clear();
                    for (T filteredObject : filteredList) {
                        add(filteredObject);
                    }
                    notifyDataSetChanged();
                } else {
                    clear();
                    filteredList.add(0, (T) "Default");
                    for (T filteredObject : filteredList) {
                        add(filteredObject);
                    }
                    notifyDataSetChanged();
                }
            }
        };

        public SimpleContainsAutocompleteAdapter(Context context, List<T> listObjects) {
            super(context, android.R.layout.simple_list_item_1, listObjects);
            this.listObjects = new ArrayList<>(listObjects);
            this.resource =  android.R.layout.simple_list_item_1;
        }

        @Override
        public Filter getFilter() {
            return mFilter;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Object listObject = getItem(position);
            ViewHolder holder;
            if (convertView != null) {
                holder = (ViewHolder) convertView.getTag();
            } else {
                convertView = LayoutInflater.from(getContext()).inflate(resource, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }

            holder.name.setText(listObject.toString());

            return convertView;
        }


        class ViewHolder {
            TextView name;

            public ViewHolder(View view) {
                name = view.findViewById(android.R.id.text1);
            }
        }
    }
  • Related