Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@akshaydashrath

akshaydashrath Feb 12, 2013

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()]);
}

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

This comment has been minimized.

Show comment
Hide comment
@jdamcd

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

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

This comment has been minimized.

Show comment
Hide comment
@orrc

orrc Feb 12, 2013

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

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