Customize EditText with delete button that clear all text when press.
1.button_delete.png
2.CustomEditText.java
import android.content.Context; import android.graphics.drawable.Drawable; import android.support.v4.content.res.ResourcesCompat; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; /** * Created by Huo Chhunleng on 8/20/2016. */ public class PEditText extends EditText { private Drawable btn_clear = ResourcesCompat.getDrawable(getResources(), R.drawable.button_delete, null); public PEditText(Context context) { super(context); init(); } public PEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public PEditText(Context context, AttributeSet attrs) { super(context, attrs); init(); } void init() { btn_clear.setBounds(0, 0, btn_clear.getIntrinsicWidth(), btn_clear.getIntrinsicHeight()); final PEditText editText = PEditText.this; this.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (editText.getCompoundDrawables()[2] == null) return false; if (event.getAction() != MotionEvent.ACTION_UP) return false; if (event.getX() > editText.getWidth() - editText.getPaddingRight() - btn_clear.getIntrinsicWidth()) { editText.setText(""); editText.setCompoundDrawables(editText.getCompoundDrawables()[0], editText.getCompoundDrawables()[1], null, editText.getCompoundDrawables()[3]); } return false; } }); this.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus){ if(!"".equals(editText.getText().toString())){ editText.setCompoundDrawables(editText.getCompoundDrawables()[0], editText.getCompoundDrawables()[1], btn_clear, editText.getCompoundDrawables()[3]); } }else { editText.setCompoundDrawables(editText.getCompoundDrawables()[0], editText.getCompoundDrawables()[1], null, editText.getCompoundDrawables()[3]); } } }); this.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (editText.getText().toString().equals("")) { editText.setCompoundDrawables(editText.getCompoundDrawables()[0], editText.getCompoundDrawables()[1], null, editText.getCompoundDrawables()[3]); } else { editText.setCompoundDrawables(editText.getCompoundDrawables()[0], editText.getCompoundDrawables()[1], btn_clear, editText.getCompoundDrawables()[3]); } } @Override public void afterTextChanged(Editable arg0) {} @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} }); } //intercept Typeface change and set it with our custom font /*public void setTypeface(Typeface tf, int style) { if (style == Typeface.BOLD) { super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Vegur-B 0.602.otf")); } else { super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Vegur-R 0.602.otf")); } }*/ }
3.et_bg_border.xml
Create an xml named et_bg_border in drawable directory.<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#FFFFFF" /> <stroke android:width="0.5dp" android:color="#2f6699" /> <corners android:topLeftRadius="5dp" android:topRightRadius="5dp" android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp" /> </shape>
4.activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp" android:orientation="vertical" android:background="#45a39c" android:gravity="center"> <CustomEditText android:layout_width="match_parent" android:layout_height="50dp" android:padding="10dp" android:inputType="textEmailAddress" android:background="@drawable/et_bg_border" android:hint="Email"/> <CustomEditText android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="10dp" android:padding="10dp" android:inputType="textPassword" android:background="@drawable/et_bg_border" android:hint="Password"/> </LinearLayout>
EmoticonEmoticon