Pravin
Pravin

Reputation: 1362

show compound drawable to EditText on event

In my application in Activity I want to set EditText as I click inside(focus) EditText and type a key clear button should appear on right side of EditText
and when EditText is empty that clear button have to removed.
But it is not showing to me..
Which event should I have to implement here..?onTouch or onFocusChange or addTextChangedListener and also what code be there..? following code I have done in activity...

in Activity :

 clear = getResources().getDrawable(R.drawable.round_clear);
     clear.setBounds(0, 0, clear.getIntrinsicWidth(), clear.getIntrinsicHeight());

and event as

@Override
public void onFocusChange(View v, boolean hasFocus) 
{
    switch (v.getId()) 
    {
        case R.id.uIDEditText:
                if(hasFocus && !uIDEditText.getText().toString().isEmpty())
                     uIDEditText.setCompoundDrawables(null, null, clear, null);                 
                else
                     uIDEditText.setCompoundDrawables(null, null, null, null);
            break;

        case R.id.pwdEditText:
                if(hasFocus && !pwdEditText.getText().toString().isEmpty())
                    pwdEditText.setCompoundDrawables(null, null, clear, null);
                else
                    pwdEditText.setCompoundDrawables(null, null, null, null);

            break;          
    }               
}


another event is :


@Override
public boolean onTouch(View v, MotionEvent event) 
{
    switch (v.getId()) 
    {
        case R.id.uIDEditText:
            final int x = (int)event.getX();
            final int y = (int)event.getY();              
            if(event.getAction() == MotionEvent.ACTION_UP && clear!=null) {
                Rect rBounds = clear.getBounds();

                int n1 = v.getRight();
                int n2 = v.getRight()+rBounds.width();
                int n3 = v.getPaddingTop();
                int n4 = v.getHeight()-v.getPaddingBottom();

                if(x>=(n1) && x<=(n2) && y>=n3 && y<=(n4))
                {
                    uIDEditText.setText("");
                    event.setAction(MotionEvent.ACTION_CANCEL);
                }
            }
    break;
       }
     }

Upvotes: 3

Views: 5730

Answers (2)

Pravin
Pravin

Reputation: 1362

I sovled it...created following code

public class CustomEditText extends EditText {

private Drawable  dRight;
private Rect rBounds;
CustomEditText(Context context,AttributeSet attributeSet){
    super(context,attributeSet);
}
@Override
public void setCompoundDrawables(Drawable left, Drawable top,
        Drawable right, Drawable bottom) {
    /*if (left != null) {
        dLeft = left;
    }*/
    if (right != null) {
        dRight = right;
    }
    super.setCompoundDrawables(left, top, right, bottom);
}

@Override
public void addTextChangedListener(TextWatcher watcher) {       
    super.addTextChangedListener(watcher);      
}

@Override
protected void onTextChanged(CharSequence text, int start,
        int lengthBefore, int lengthAfter) {        
    super.onTextChanged(text, start, lengthBefore, lengthAfter);
    if(this.getText().toString().length()>0)
        this.setCompoundDrawablesWithIntrinsicBounds(null, null, dRight, null);
    else
        this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
}   
@Override
protected void finalize() throws Throwable {
    dRight = null;
    rBounds = null;
    super.finalize();
}
 }

and added in xml:

<com.example.screen.CustomEditText
    android:id="@+id/uIDEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"       
    android:drawableRight="@drawable/round_clear"       
    android:textColor="#ffffff" />


in Activity (edittext ontouch listener):

 uIDEditText.setOnTouchListener(new OnTouchListener() {         
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() != MotionEvent.ACTION_UP)                         
                return false;

            if (event.getX() > uIDEditText.getWidth()  - clear.getIntrinsicWidth()) 
            {
                uIDEditText.setText("");    
                event.setAction(MotionEvent.ACTION_CANCEL);
            }
            return false;   
        }
    });

Upvotes: 8

itsrajesh4uguys
itsrajesh4uguys

Reputation: 4638

What you want is here .

Use Textwatcher functionality for edittext.

package com.example.editwatch;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends Activity {


    EditText edittext_search;
    ImageView imageView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edittext_search=(EditText)findViewById(R.id.editText1);
        imageView1=(ImageView)findViewById(R.id.imageView1);

        edittext_search.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

                if(edittext_search.getText().length()>0)
                {
                    imageView1.setVisibility(View.VISIBLE);
                }
                else
                {
                    imageView1.setVisibility(View.GONE);
                }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });


    }

}

Upvotes: 0

Related Questions