Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get input text with customview without edittext android

I have created customview. Whenever user double taps on the view it should display the keyboard and user can draw new Text.

Holder is a customview which extends view. But it's showing the keyboard. How to get the text?

 public Holder(Context context, AttributeSet attrs) {
    super(context, attrs);
    Log.e(TAG,"EXE");
    imm = (InputMethodManager)
           context. getSystemService(Context.INPUT_METHOD_SERVICE);



 public boolean onDoubleTap(MotionEvent e) {        

        View view = Holder.this.getRootView();
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
     // imm.showSoftInput(Holder.this, InputMethodManager.SHOW_FORCED);  not working
like image 948
Asthme Avatar asked Dec 31 '14 07:12

Asthme


Video Answer


2 Answers

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;
    }
}
like image 82
mmlooloo Avatar answered Oct 13 '22 14:10

mmlooloo


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();


                }
            });
like image 44
NightCrawler Avatar answered Oct 13 '22 15:10

NightCrawler