I'm working at an app, and I'm trying to make a gmail like "To" field, which has blocks inside which cannot be edited once added, but just removed entirely (like in the attached image). If it can have an image too, that would be perfect.
Asked
Active
Viewed 1.8k times
38

Ciprian
- 2,879
- 3
- 28
- 28
-
1Check this out: http://stackoverflow.com/questions/11318551/creating-a-custom-edittext-with-tag-like-feature – Flávio Faria Dec 06 '12 at 16:29
-
HI Did u achieved this Please let me know. i am also trying to implement like this, but getting somany problems @Ciprian – AndroidDev Sep 02 '13 at 11:08
-
In the end it worked, but it gave me a lot of headaches at the time :) – Ciprian Sep 03 '13 at 12:25
-
@Ciprian Hey Please Post sample code if u have .. other wise please answer this qn. if u know.. http://stackoverflow.com/questions/18780756/android-issue-with-image-span-replacement-in-different-android-versions-for-the – AndroidDev Sep 18 '13 at 07:19
-
Just follow the instructions of CommonsWare, that pointed me to finding the solution – Ciprian Sep 18 '13 at 15:24
-
I have some issue. Please hv a look http://stackoverflow.com/questions/19676347/setting-multiple-custom-elements-to-multiautocompletetextview-android – Braj Nov 06 '13 at 06:42
2 Answers
30
This technique -- referred to as "chips" -- is discussed by Roman Nurik in a Google+ post. He, in turn, points to Macarse's answer here on StackOverflow. They, in turn, point to the implementation of this UI that you see in the stock messaging client.

Community
- 1
- 1

CommonsWare
- 986,068
- 189
- 2,389
- 2,491
-
@Gopinath: AFAIK, yes. If you are having problems with getting it to work, open up a new StackOverflow question and supply the details. – CommonsWare Dec 10 '12 at 14:37
-
@CommonsWare , I tried to make use of the chips UI following the Google+ post. It has lot of referrences to the Android4.0 framework classes and I gave it up. Since then, there seems to have been better samples provided in the community. One of my preferred answers is http://stackoverflow.com/questions/10812316/contact-bubble-edittext/10864568#10864568 – Gopinath Dec 10 '12 at 14:43
-
@CommonsWare you are right but it is very hard to understand and implement the code in stock mms application. I could not do it. I wish it can be reused easy – tasomaniac Feb 21 '13 at 14:50
-
@CommonsWare I know but customizing it and using an populating it with an online data seems hard to me. – tasomaniac Feb 21 '13 at 20:37
-
@CommonsWare i can not import https://github.com/Derelicte/android_frameworks/tree/master/ex/chips . i think those are developed in ndk environment. i am unable to test or use that code. Any Straight or good tutorial for chips implementation in multi autocomplete textview – AndroidDev Sep 02 '13 at 11:12
-
@CommonsWare please check the link for my SO Question . http://stackoverflow.com/questions/18561858/how-add-and-delete-the-contact-bubbles-properly-in-multiautocompletetextview-wit – AndroidDev Sep 02 '13 at 11:13
11
I open-sourced our solution TokenAutoComplete on github. Mine has been tested back to 2.2. I designed my code to allow pretty simple implementations and customizations.
Here's an example implementation using my library:
Subclass TokenCompleteTextView
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.getEmail());
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 code for contact_token (you can use any kind of layout here or could throw an ImageView in if you want images in the token)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/token_background"
android:padding="5dp"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
Token backgound drawable
<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; }
}
Sample activity
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.setPrefix("To: ");
}
}
Layout code
<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>

Marshall Weir
- 463
- 5
- 10
-
I have some issue. Please hv a look http://stackoverflow.com/questions/19676347/setting-multiple-custom-elements-to-multiautocompletetextview-android – Braj Nov 06 '13 at 06:42
-