I think I found a quite elegant solution:
I've used a Drawable in the textbox (in my case drawableRight), and I've assigned a click listener just on the drawable that executes the switch between numeric and text mode.
I'm able to assign a listener just on the drawable using a little trick taken from Handling click events on a drawable within an EditText :
public class MyEdittextextends EditText {
private Drawable drawableRight;
private Drawable drawableLeft;
private Drawable drawableTop;
private Drawable drawableBottom;
//YOUR STUFF HERE
@Override
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
if (right != null) {
drawableRight = right;
}
if (left != null) {
drawableLeft = left;
}
super.setCompoundDrawables(left, top, right, bottom);
}
View.OnClickListener _leftDrawableClickListener = null;
View.OnClickListener _rightDrawableClickListener = null;
public void setLeftDrawableClickListener(View.OnClickListener clickListener) {
_leftDrawableClickListener = clickListener;
}
public void setRightDrawableClickListener(View.OnClickListener clickListener) {
_rightDrawableClickListener = clickListener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int x, y;
Rect bounds;
x = (int) event.getX();
y = (int) event.getY();
// this works for left since container shares 0,0 origin with bounds
if (drawableLeft != null) {
bounds = drawableLeft.getBounds();
if (bounds.contains(x - fuzz, y - fuzz)) {
try {
_leftDrawableClickListener.onClick(this);
} catch (Exception e) {
}
if (consumeEvent) {
event.setAction(MotionEvent.ACTION_CANCEL);
return false;
}
}
} else if (drawableRight != null) {
bounds = drawableRight.getBounds();
if (x >= (this.getRight() - bounds.width() - fuzz) && x <= (this.getRight() - this.getPaddingRight() + fuzz) && y >= (this.getPaddingTop() - fuzz) && y <= (this.getHeight() - this.getPaddingBottom()) + fuzz) {
try {
_rightDrawableClickListener.onClick(this);
} catch (Exception e) {
}
if (consumeEvent) {
event.setAction(MotionEvent.ACTION_CANCEL);
return false;
}
}
} else if (drawableTop != null) {
// not implemented yet
} else if (drawableBottom != null) {
// not implemented yet
}
}
return super.onTouchEvent(event);
}
@Override
protected void finalize() throws Throwable {
drawableRight = null;
drawableBottom = null;
drawableLeft = null;
drawableTop = null;
super.finalize();
}
}
Once the custom EditText has been created, I used this code in my Activity
myEdittext = (EditText) findViewById(R.id.myEdittext);
myEdittext.setRightDrawableClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (myEdittext.getInputType() != InputType.TYPE_CLASS_TEXT) {
myEdittext.setInputType(InputType.TYPE_CLASS_TEXT);
myEdittext.setRawInputType(InputType.TYPE_CLASS_TEXT);
myEdittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.keyboard_123, 0);
} else {
myEdittext.setRawInputType(InputType.TYPE_CLASS_NUMBER);
myEdittext.setInputType(InputType.TYPE_CLASS_NUMBER);
myEdittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.keyboard_abc, 0);
}
}
});
this is the result: when the EditText is first shown appears like this

and clicking on "ABC" image becomes like this

hope this can help someone