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;
}
}