Skip to content

Instantly share code, notes, and snippets.

@jdamcd
Last active December 12, 2015 07:48
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdamcd/4738788 to your computer and use it in GitHub Desktop.
Save jdamcd/4738788 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link
Author

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".

@jdamcd
Copy link
Author

jdamcd commented Feb 12, 2013

@orrc
Copy link

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