Get input text with customview without edittext android
Try to create edittext dynamically and send the value to view.Here is my code.its best alternative ..
public class CustomEditText extends EditText {
private KeyImeChange keyImeChangeListener;
public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomEditText(Context context) {
super(context);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setKeyImeChangeListener(KeyImeChange listener){
keyImeChangeListener = listener;
}
public interface KeyImeChange {
public void onKeyIme(int keyCode, KeyEvent event);
}
@Override
public boolean onKeyPreIme (int keyCode, KeyEvent event){
if(keyImeChangeListener != null){
keyImeChangeListener.onKeyIme(keyCode, event);
}
return false;
}
}
MainActivity.editText2.setKeyImeChangeListener(new CustomEditText.KeyImeChange() {
@Override
public void onKeyIme(int keyCode, KeyEvent event) {
//Update view on keyboard close
invalidate();
}
});
Here is a custom FrameLayout
that when you click on it, it shows soft-keyboard
and you can type any thing and when you press the enter it shows a toast text with what you have typed, I hope it will give you the idea:
public class MyCustomFrameLayout extends FrameLayout {
String mText;
public MyCustomFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyCustomFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyCustomFrameLayout(Context context) {
super(context);
init();
}
@Override
public boolean onCheckIsTextEditor() {
return true;
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
BaseInputConnection fic = new BaseInputConnection(this, false);
outAttrs.actionLabel = null;
outAttrs.inputType = InputType.TYPE_NULL;
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
return fic;
}
public void init(){
setFocusable(true);
setFocusableInTouchMode(true);
mText ="";
setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if((keyCode >= KeyEvent.KEYCODE_A) && (keyCode <= KeyEvent.KEYCODE_Z)) {
mText = mText + (char) event.getUnicodeChar();
return true;
}
else if(keyCode >= KeyEvent.KEYCODE_ENTER){
Toast.makeText(getContext(), "The text is: " + mText , Toast.LENGTH_LONG).show();
return true;
}
}
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(this, InputMethodManager.SHOW_FORCED);
}
return true;
}
public String getText(){
return mText;
}
}