Android AutoComplete

I'm still new to Android, and I'm trying to work with autocomplete text fields. I use MultiAutoCompleteTextView to populate a text box and provide tooltips from an array of strings. Each row in the array is the name of the object with an identifier. So my question is twofold:

  • After a user clicks on a given entry in autocomplete, how can I find the identifier corresponding to the line selected by the user?

  • Is it possible to create a “Facebook-like block” around an element that is selected from an autocomplete list that works like atomic units that the user can delete by pressing the X button? (similar to what happens in every tag in the tag field here in stackoverflow)

Thank you in advance

+4
source share
3 answers

Android has source code for the Chips widgets that are used in the Mail app. These are chips that represent users who are the recipients of a message. And they look just like the Facebook widgets you are referring to: a name with an “X” to cancel. I was able to customize the code and make it suitable for my needs, but to be honest, it is really complicated, and it took me a lot of time to wrap my head around me.

The basic principle is that you use Spannable strings and draw bitmaps for the background and "X" manually.

Here is the source code for Android: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/com/android/ex/chips/RecipientEditTextView.java

Main methods: createSelectedChip , constructChipSpan .

+4
source

I open-sourced TokenAutoComplete on github that solves this pretty well. There seems to be no simpler answer. Here is a basic implementation of what you are describing:

 public class ContactsCompletionView extends TokenCompleteTextView { public ContactsCompletionView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected View getViewForObject(Object object) { Person p = (Person)object; LayoutInflater l = (LayoutInflater)getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE); LinearLayout view = (LinearLayout)l.inflate(R.layout.contact_token, (ViewGroup)ContactsCompletionView.this.getParent(), false); ((TextView)view.findViewById(R.id.name)).setText(p.getName()); return view; } @Override protected Object defaultObject(String completionText) { //Stupid simple example of guessing if we have an email or not int index = completionText.indexOf('@'); if (index == -1) { return new Person(completionText, completionText.replace(" ", "") + "@example.com"); } else { return new Person(completionText.substring(0, index), completionText); } } } 

Layout format for contact_token (you will need to find your own x)

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/token_background"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" android:textSize="14sp" android:text="Test Me" android:padding="2dp" /> <ImageView android:layout_height="10dp" android:layout_width="10dp" android:src="@drawable/x" android:layout_gravity="center_vertical" android:layout_marginLeft="3dp" android:layout_marginRight="5dp" /> </LinearLayout> 

Token Return Hole

 <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#ffafafaf" /> <corners android:topLeftRadius="5dp" android:bottomLeftRadius="5dp" android:topRightRadius="5dp" android:bottomRightRadius="5dp" /> </shape> 

Person Object Code

 public class Person implements Serializable { private String name; private String email; public Person(String n, String e) { name = n; email = e; } public String getName() { return name; } public String getEmail() { return email; } @Override public String toString() { return name; } } 

Activity Example

 public class TokenActivity extends Activity { ContactsCompletionView completionView; Person[] people; ArrayAdapter<Person> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); people = new Person[]{ new Person("Marshall Weir", " marshall@example.com "), new Person("Margaret Smith", " margaret@example.com "), new Person("Max Jordan", " max@example.com "), new Person("Meg Peterson", " meg@example.com "), new Person("Amanda Johnson", " amanda@example.com "), new Person("Terry Anderson", " terry@example.com ") }; adapter = new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, people); completionView = (ContactsCompletionView)findViewById(R.id.searchView); completionView.setAdapter(adapter); completionView.setTokenClickStyle(TokenCompleteTextView.TokenClickStyle.Delete); } } 

Layout format

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.tokenautocomplete.ContactsCompletionView android:id="@+id/searchView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> 

Here you get the interface:

Image of working TokenAutoComplete activity

+4
source

I do not know if this link can help you with what you want, but just in case, take a look at them.

AutoFill 1

AutoComplete 2

I hope they help something.

+1
source

All Articles