almost 11 years ago
In android there is no default text clear button functionality in EditText so here is a simple example of implementing text clear button in edit-text, You just have to follow instruction given below to make it in your edit-text.
1)- First you have to create a class MyEditText.jave
- public class MyEditText extends EditText {
- private static final int MAX_LENGTH = 102;
- public MyEditText(Context context, AttributeSet attrs) {
- super(context, attrs);
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
- this.setBackgroundResource(android.R.drawable.edit_text);
- }
- setOnEditorActionListener(new OnEditorActionListener() {
- @Override
- public synchronized boolean onEditorAction(TextView v,
- int actionId, KeyEvent event) {
- return false;
- }
- });
- String value = "";
- final String viewMode = "editing";
- final String viewSide = "right";
- final Drawable x = getResources().getDrawable(R.drawable.cross);
- // The height will be set the same with [X] icon
- //setHeight(x.getBounds().height());
- x.setBounds(0, 0, x.getIntrinsicWidth(), x.getIntrinsicHeight());
- Drawable x2 = viewMode.equals("never") ? null : viewMode
- .equals("always") ? x : viewMode.equals("editing") ? (value
- .equals("") ? null : x)
- : viewMode.equals("unlessEditing") ? (value.equals("") ? x
- : null) : null;
- // Display search icon in text field
- setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (getCompoundDrawables()[viewSide.equals("left") ? 0 : 2] == null) {
- return false;
- }
- if (event.getAction() != MotionEvent.ACTION_UP) {
- return false;
- }
- // x pressed
- if ((viewSide.equals("left") && event.getX() < getPaddingLeft()
- + x.getIntrinsicWidth())
- || (viewSide.equals("right") && event.getX() > getWidth()
- - getPaddingRight() - x.getIntrinsicWidth())) {
- Drawable x3 = viewMode.equals("never") ? null : viewMode
- .equals("always") ? x
- : viewMode.equals("editing") ? null : viewMode
- .equals("unlessEditing") ? x : null;
- setText("");
- setCompoundDrawables(null, null,
- viewSide.equals("right") ? x3 : null, null);
- }
- return false;
- }
- });
- addTextChangedListener(new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence s, int start, int before,
- int count) {
- Drawable x4 = viewMode.equals("never") ? null : viewMode
- .equals("always") ? x
- : viewMode.equals("editing") ? (getText().toString()
- .equals("") ? null : x) : viewMode
- .equals("unlessEditing") ? (getText()
- .toString().equals("") ? x : null) : null;
- setCompoundDrawables(null, null,
- viewSide.equals("right") ? x4 : null, null);
- }
- @Override
- public void afterTextChanged(Editable s) {
- if (s != null && s.length() > MAX_LENGTH) {
- setText(s.subSequence(0, MAX_LENGTH));
- setSelection(MAX_LENGTH);
- }
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- }
- });
- }
- }
public class MyEditText extends EditText { private static final int MAX_LENGTH = 102; public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { this.setBackgroundResource(android.R.drawable.edit_text); } setOnEditorActionListener(new OnEditorActionListener() { @Override public synchronized boolean onEditorAction(TextView v, int actionId, KeyEvent event) { return false; } }); String value = ""; final String viewMode = "editing"; final String viewSide = "right"; final Drawable x = getResources().getDrawable(R.drawable.cross); // The height will be set the same with [X] icon //setHeight(x.getBounds().height()); x.setBounds(0, 0, x.getIntrinsicWidth(), x.getIntrinsicHeight()); Drawable x2 = viewMode.equals("never") ? null : viewMode .equals("always") ? x : viewMode.equals("editing") ? (value .equals("") ? null : x) : viewMode.equals("unlessEditing") ? (value.equals("") ? x : null) : null; // Display search icon in text field setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (getCompoundDrawables()[viewSide.equals("left") ? 0 : 2] == null) { return false; } if (event.getAction() != MotionEvent.ACTION_UP) { return false; } // x pressed if ((viewSide.equals("left") && event.getX() < getPaddingLeft() + x.getIntrinsicWidth()) || (viewSide.equals("right") && event.getX() > getWidth() - getPaddingRight() - x.getIntrinsicWidth())) { Drawable x3 = viewMode.equals("never") ? null : viewMode .equals("always") ? x : viewMode.equals("editing") ? null : viewMode .equals("unlessEditing") ? x : null; setText(""); setCompoundDrawables(null, null, viewSide.equals("right") ? x3 : null, null); } return false; } }); addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Drawable x4 = viewMode.equals("never") ? null : viewMode .equals("always") ? x : viewMode.equals("editing") ? (getText().toString() .equals("") ? null : x) : viewMode .equals("unlessEditing") ? (getText() .toString().equals("") ? x : null) : null; setCompoundDrawables(null, null, viewSide.equals("right") ? x4 : null, null); } @Override public void afterTextChanged(Editable s) { if (s != null && s.length() > MAX_LENGTH) { setText(s.subSequence(0, MAX_LENGTH)); setSelection(MAX_LENGTH); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } }); } }
2)- Call this class in your activity like this.
3)- Now you just have to make changes in your .xml file in your project.
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:ignore="MergeRootFrame" >
- <com.receiptbook.android.utility.MyEditText
- android:id="@+id/search_edittext"
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:layout_marginTop="15dip"
- android:drawableLeft="@drawable/icon_search"
- android:inputType="textCapWords"
- android:padding="15dip"
- android:singleLine="true" />
- </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:ignore="MergeRootFrame" > <com.receiptbook.android.utility.MyEditText android:id="@+id/search_edittext" android:layout_width="fill_parent" android:layout_height="50dip" android:layout_marginTop="15dip" android:drawableLeft="@drawable/icon_search" android:inputType="textCapWords" android:padding="15dip" android:singleLine="true" /> </RelativeLayout>
Thats it hope you like it..:)
0 Comment(s)