Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Android create custom edittext with clear button.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.71k
    Comment on it

    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&#95;INT < Build.VERSION&#95;CODES.ICE&#95;CREAM&#95;SANDWICH) {
            this.setBackgroundResource(android.R.drawable.edit&#95;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&#95;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&#95;LENGTH) {
                                    setText(s.subSequence(0, MAX&#95;LENGTH));
                                    setSelection(MAX&#95;LENGTH);
                                }
                            }
    
                            @Override
                            public void beforeTextChanged(CharSequence s, int start, int count,
                                    int after) {
                            }
                        });
         }
    }
    

    2)- Call this class in your activity like this.

    MyEditText  searchEditText=(MyEditText)findViewById(R.id.search&#95;edittext);
    

    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&#95;width="match&#95;parent"
    android:layout&#95;height="match&#95;parent"
    tools:ignore="MergeRootFrame" >
    
    <com.receiptbook.android.utility.MyEditText
        android:id="@+id/search&#95;edittext"
        android:layout&#95;width="fill&#95;parent"
        android:layout&#95;height="50dip"
        android:layout&#95;marginTop="15dip"
        android:drawableLeft="@drawable/icon&#95;search"
        android:inputType="textCapWords"
        android:padding="15dip"
        android:singleLine="true" />
    
    
    
     </RelativeLayout>
    

    Thats it hope you like it..:)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: