Skip to content

Instantly share code, notes, and snippets.

@letroll
Last active July 6, 2017 11:42
Show Gist options
  • Save letroll/de3f3005893fdfc1803e to your computer and use it in GitHub Desktop.
Save letroll/de3f3005893fdfc1803e to your computer and use it in GitHub Desktop.
[EmailAutoCompleteTextView] EmailAutoCompleteTextView #android #customView #email
package com.example.customView;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AutoCompleteTextView;
/**
* Created by julien quievreux on 06/08/2014.
*/
public class EmailAutoCompleteTextView extends AutoCompleteTextView {
/**
* VARIABLES *
*/
public final static String TAG = "EmailAutoCompleteView";
private boolean mShowClear = false;
/**
* OBJECTS *
*/
private Drawable imgCloseButton = getResources().getDrawable(R.drawable.closebtn);
public EmailAutoCompleteTextView(Context context) {
super(context);
initializeClearButton(context, null);
}
public EmailAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initializeClearButton(context, attrs);
}
/**
* Constructor
*
* @param attrs use xml attribute with xmlns:edittextwithcross="http://schemas.android.com/apk/res-auto"
*/
public EmailAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initializeClearButton(context, attrs);
}
private void initializeClearButton(Context context, AttributeSet attrs) {
if (!isInEditMode()) {
if (context != null) {
setThreshold(1);
setAdapter(Utils.getEmailAddressAdapter(context));
}
if (attrs != null) {
TypedArray typedArray = context
.obtainStyledAttributes(attrs, R.styleable.EditTextWithCross);
mShowClear = typedArray.getBoolean(R.styleable.EditTextWithCross_showClearIcon, false);
int resIcon = typedArray.getInteger(R.styleable.EditTextWithCross_crossIconRes, -1);
if (resIcon != -1)
imgCloseButton = getResources().getDrawable(resIcon);
}
if (mShowClear) {
// Set bounds of the Clear button so it will look ok
imgCloseButton.setBounds(0, 0, imgCloseButton.getIntrinsicWidth(),
imgCloseButton.getIntrinsicHeight());
// There may be initial text in the field, so we may need to display the button
handleClearButton();
// if the Close image is displayed and the user remove his finger from the button, clear
// it. Otherwise do nothing
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (getCompoundDrawables()[2] == null) {
return false;
}
if (event.getAction() != MotionEvent.ACTION_UP) {
return false;
}
if (event.getX() > getWidth() - getPaddingRight() - imgCloseButton.getIntrinsicWidth()) {
setText("");
handleClearButton();
}
return false;
}
});
// if text changes, take care of the button
addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
handleClearButton();
}
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
}
}
}
public boolean ismShowClear() {
return mShowClear;
}
public void setmShowClear(boolean mShowClear) {
this.mShowClear = mShowClear;
}
private void handleClearButton() {
if (this.getText().toString().equals("")) {
// remove clear button
setCompoundDrawables(this.getCompoundDrawables()[0], getCompoundDrawables()[1], null, getCompoundDrawables()[3]);
} else {
// add the clear button
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], imgCloseButton, getCompoundDrawables()[3]);
}
}
}
/**
* require (API level 5+) and :
* <uses-permission android:name="android.permission.GET_ACCOUNTS" />
*
* @param context
* @return an ArrayAdapter<String> which contain Email account
*/
public static ArrayAdapter<String> getEmailAddressAdapter(Context context) {
Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(context).getAccounts();
HashSet<String> emailSet = new HashSet<>();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
emailSet.add(account.name);
}
}
String[] emailArray = emailSet.toArray(new String[emailSet.size()]);
return new ArrayAdapter<>(context, android.R.layout.simple_dropdown_item_1line, emailArray);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment