Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Created by Rany Albeg Wein on 24/01/2016.
*/
public class EmailCompletionEditText extends MultiAutoCompleteTextView {
private ContentResolver mContentResolver;
public EmailCompletionEditText(Context context) {
super(context);
init(context);
}
public EmailCompletionEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public EmailCompletionEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public EmailCompletionEditText(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
private void init(Context context) {
mContentResolver = context.getContentResolver();
setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
final String[] from = new String[]{ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.ADDRESS,
ContactsContract.Contacts.Photo.PHOTO_URI};
final int[] to = new int[]{R.id.tv_contact_name,
R.id.tv_contact_email,
R.id.iv_contact_photo};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, R.layout.contact_row, null, from, to, 0) {
@Override
public CharSequence convertToString(Cursor cursor) {
final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS);
return cursor.getString(emailIndex);
}
};
adapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
if (constraint == null) {
return null;
}
String query = constraint.toString();
final String selection = ContactsContract.Contacts.DISPLAY_NAME
+ " LIKE ? "
+ " OR "
+ ContactsContract.CommonDataKinds.Email.ADDRESS
+ " LIKE ? ";
String[] selectionArgs = new String[]{"%" + query + "%"
, "%" + query + "%"};
return mContentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, selection, selectionArgs, null);
}
});
setAdapter(adapter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.