set EditText With Clear Button - Android User Interface

Android examples for User Interface:EditText

Description

set EditText With Clear Button

Demo Code


//package com.java2s;

import android.content.Context;

import android.graphics.drawable.Drawable;

import android.text.Editable;

import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;

import android.view.MotionEvent;
import android.view.View;

import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;

import android.widget.EditText;

public class Main {

    public static void setEditWithClearButton(final EditText edt,
            final int imgRes) {

        edt.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override//from   w w w  . j  a v  a2  s  .  c om
            public void onFocusChange(View v, boolean hasFocus) {
                Drawable[] drawables = edt.getCompoundDrawables();
                if (hasFocus && edt.getText().toString().length() > 0) {
                    edt.setTag(true);

                    edt.setCompoundDrawablesWithIntrinsicBounds(
                            drawables[0], drawables[1], edt.getContext()
                                    .getResources().getDrawable(imgRes),
                            drawables[3]);

                } else {
                    edt.setTag(false);
                    edt.setCompoundDrawablesWithIntrinsicBounds(
                            drawables[0], drawables[1], null, drawables[3]);
                }
            }
        });
        final int padingRight = Dip2Px(edt.getContext(), 50);
        edt.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    int curX = (int) event.getX();
                    if (curX > v.getWidth() - padingRight
                            && !TextUtils.isEmpty(edt.getText())) {
                        if (edt.getTag() != null && (Boolean) edt.getTag()) {
                            edt.setText("");
                            int cacheInputType = edt.getInputType();
                            edt.setInputType(InputType.TYPE_NULL);
                            edt.onTouchEvent(event);
                            edt.setInputType(cacheInputType);
                            return true;
                        } else {
                            return false;
                        }
                    }
                    break;
                }
                return false;
            }
        });

        edt.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {

                Drawable[] drawables = edt.getCompoundDrawables();
                if (edt.getText().toString().length() == 0) {
                    edt.setTag(false);
                    edt.setCompoundDrawablesWithIntrinsicBounds(
                            drawables[0], drawables[1], null, drawables[3]);

                } else {
                    edt.setTag(true);
                    edt.setCompoundDrawablesWithIntrinsicBounds(
                            drawables[0], drawables[1], edt.getContext()
                                    .getResources().getDrawable(imgRes),
                            drawables[3]);

                }
            }
        });

    }

    /** dip to px */
    public static int Dip2Px(Context c, int dipValue) {
        float scale = c.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5F);
    }

    /** dip to px */
    public static float Dip2Px(Context c, float dipValue) {
        float scale = c.getResources().getDisplayMetrics().density;
        return dipValue * scale + 0.5F;
    }
}

Related Tutorials