Create a gist now

Instantly share code, notes, and snippets.

Email adapter for AutoCompleteTextView
private ArrayAdapter<String> getEmailAddressAdapter(Context context) {
Account[] accounts = AccountManager.get(context).getAccountsByType("com.google");
String[] addresses = new String[accounts.length];
for (int i = 0; i < accounts.length; i++) {
addresses[i] = accounts[i].name;
}
return new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, addresses);
}
@akshaydashrath

A small improvement on your idea

private String[] getListOfUserNames(Context context) {
HashSet emailIdsWithoutDuplicates = new HashSet();
Account[] accounts = AccountManager.get(context.getApplicationContext()).getAccounts();
for (int i = 0; i < accounts.length; i++) {
String name = accounts[i].name;
if (name.contains("@")) {
emailIdsWithoutDuplicates.add(name);
}
}
return getArrayFromSet(emailIdsWithoutDuplicates);
}

public static String[] getArrayFromSet(HashSet stringSet) {
return stringSet.toArray(new String[stringSet.size()]);
}

@jdamcd
Owner
jdamcd commented Feb 12, 2013

Nice. Would probably even use an email regex against the account names. There might be some crazy Twitter clients that use account names like "@jdamcd".

@orrc
orrc commented Feb 12, 2013

Nice.. I came here to suggest exactly the same things, but they're already implemented! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment