Edit text with clear button in Android app

This simple example would help you to create a edit text with clear button


EditText editText = (EditText) findViewById(R.id.Editdob);


drawable created to assign in edit text (in Activity/Fragment)

final Drawable drawable = getResources().getDrawable(
R.drawable.clear_edit_text);


drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
editText.setCompoundDrawables(null, null, drawable, null);
ImageView crossImageView = new ImageView(this);
crossImageView.setImageDrawable(drawable);




On touch listener for edit text (in Activity/Fragment)


editText.setOnTouchListener(new OnTouchListener() {


@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getX() > editText.getWidth()
- drawable.getIntrinsicWidth() - 10)
editText.setText("");
}
return false;
}
});

Comments

Post a Comment

Popular posts from this blog

Input filter (space not allowed) for EditText

Android Versions details

English - Formal and Informal words