I am an android and iphone developer. I am a newbie though working on this huge project. I am trying to implement the leftView, leftviewmode,rightview,rightviewmode properties of the textfield in iphone in android.
I was thinking that with the leftview and rightview the setCompoundDrawables() method could meet the challenge. However, I am stumped as to how to implement the various modes; "Never", "always" etc.
Am I on the right track? Could anyone please help?
Thanks
This code will give you an Android EditText field that functions exactly like an iPhone UITextField with options for clearbuttonmode, leftview, rightview, leftviewmode, rightviewmode, UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways
String value = "";//any text you are pre-filling in the EditText
final String viewMode = "editing";//never | editing | unlessEditing | always
final String viewSide = "right"; //left | right
final EditText et = new EditText(this);
et.setText(value);
//your leftview, rightview or clearbuttonmode image. for clearbuttonmode this one from standard android images looks pretty good actually
final Drawable x = getResources().getDrawable(R.drawable.presence_offline);
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;
et.setCompoundDrawables(viewSide.equals("left")?x2:null, null, viewSide.equals("right")?x2:null, null);
et.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (et.getCompoundDrawables()[viewSide.equals("left")?0:2] == null) {
return false;
}
if (event.getAction() != MotionEvent.ACTION_UP) {
return false;
}
//x pressed
if ((viewSide.equals("left")&&event.getX() < et.getPaddingLeft() + x.getIntrinsicWidth())
||(viewSide.equals("right")&&event.getX() > et.getWidth() - et.getPaddingRight() - x.getIntrinsicWidth())) {
Drawable x3 = viewMode.equals("never")?null:viewMode.equals("always")?x:viewMode.equals("editing")?null:viewMode.equals("unlessEditing")?x:null;
et.setText("");
et.setCompoundDrawables(viewSide.equals("left")?x3:null, null, viewSide.equals("right")?x3:null, null);
}
return false;
}
});
et.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")?(et.getText().toString().equals("") ? null : x):viewMode.equals("unlessEditing")?(et.getText().toString().equals("") ? x : null):null;
et.setCompoundDrawables(viewSide.equals("left")?x4:null, null, viewSide.equals("right")?x4:null, null);
}
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With