Get input text with customview without edittext android
Asked Answered
G

9

15

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
Greenness answered 31/12, 2014 at 7:7 Comment(1)
I know this is an old question. But if anyone faces the issue now, you have to make the view focusable. Just set "focusable=true" and "focusableInTouchMode=true" and it will do the trick.Rubierubiginous
I
6

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


                }
            });
Intranuclear answered 6/1, 2015 at 5:24 Comment(0)
S
9

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;
    }
}
Stets answered 2/1, 2015 at 16:21 Comment(5)
No, but you can do it easily, after receiving each key down call invalidates and in onDraw method write mText.Stets
i am extending view .i have added these code there.The keyboard is not changing to Ime Action DoneGreenness
i put a log there .its not going oncreateInputConnection(); and i am invoking keyboard using this method imm.toggleSoftInput(0, 0);Greenness
can you post your custom view so I can try it?Stets
I think the bug is here imm.showSoftInput(view,InputMethodManager.SHOW_FORCED); because the root view has not set onCheckIsTextEditor or other methods.Stets
I
6

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


                }
            });
Intranuclear answered 6/1, 2015 at 5:24 Comment(0)
S
2

@Asthme: After checking the link at How to Capture soft keyboard input in a View?, in order to make a View editable you Also needs to make custom BaseInputConnection to make View editable.

And the possible code solution is given at below link that has input from Dianne Hackborne(Android Framework Engineer).

https://groups.google.com/forum/#!topic/android-developers/N0ITpZZ16Bw

Go till last of this link and you will find custom BaseInputConnection to make View editable.

I am posting relevant code and screen shots for sample code:

class MyInputConnection extends BaseInputConnection {
    private SpannableStringBuilder myeditable;
    Holder mycustomview;

    public MyInputConnection(View targetView, boolean fullEditor) {
        super(targetView, fullEditor);
        mycustomview = (Holder) targetView;
    }

    public Editable getEditable() {
        if (myeditable == null) {
            myeditable = (SpannableStringBuilder) Editable.Factory.getInstance()
            .newEditable("Placeholder");
        }
        return myeditable;
    }

    public boolean commitText(CharSequence text, int newCursorPosition) {
        invalidate();
        myeditable.append(text);
        mycustomview.setText(text);
        return true;
    }
}

Since this is Custom view, we need to provide implementation of setText.

    public void setText(CharSequence text) {
                mText = text;
                requestLayout();
                invalidate();
            }

Below is the complete custom view code, this differs from your implementation (I am leaving Gesture* API Unimplemented)

     package com.example.soappdemo;

        import android.content.Context;
        import android.graphics.Canvas;
        import android.graphics.Paint;
        import android.text.Editable;
        import android.text.InputType;
        import android.text.SpannableStringBuilder;
        import android.util.AttributeSet;
        import android.util.Log;
        import android.view.GestureDetector;
        import android.view.KeyEvent;
        import android.view.MotionEvent;
        import android.view.View;
        import android.view.inputmethod.BaseInputConnection;
        import android.view.inputmethod.EditorInfo;
        import android.view.inputmethod.InputConnection;
        import android.view.inputmethod.InputMethodManager;
        import android.widget.TextView;

        public class Holder extends View implements GestureDetector.OnDoubleTapListener {

            InputMethodManager imm;
            private Paint paint;
            private static final String TAG="Holder";
             private CharSequence mText="original";
            public Holder(Context context, AttributeSet attrs) {
                super(context, attrs);
                init();
                //requestFocus();
                setFocusableInTouchMode(true);
                setFocusable(true);
                requestFocus();


                setOnKeyListener(new OnKeyListener() {
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        Log.d(TAG, "onKeyListener");
                        if (event.getAction() == KeyEvent.ACTION_DOWN) {
                            // Perform action on key press
                            Log.d(TAG, "ACTION_DOWN");
                            return true;
                        }
                        return false;
                    }
                });

            }

             public void init(){
                    paint = new Paint();
                    paint.setTextSize(12);
                    paint.setColor(0xFF668800);
                    paint.setStyle(Paint.Style.FILL);
                }

             @Override
                protected void onDraw(Canvas canvas) {
                    canvas.drawText(mText.toString(), 100, 100, paint);
                }

                @Override
                protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                    this.setMeasuredDimension(150,200);     
                }

                public void setText(CharSequence text) {
                    mText = text;
                    requestLayout();
                    invalidate();
                }




            public boolean onTouchEvent(MotionEvent event) {
                super.onTouchEvent(event);
                Log.d(TAG, "onTOUCH");
                if (event.getAction() == MotionEvent.ACTION_UP) {

                    // show the keyboard so we can enter text
                    InputMethodManager imm = (InputMethodManager) getContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(this,0);
                }
                return true;
            }

            @Override
            public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
                Log.d(TAG, "onCreateInputConnection");

                outAttrs.actionLabel = null;
                outAttrs.label="TEST TEXT";
                outAttrs.inputType = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
                outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;

                return new MyInputConnection(this,true);

            }

            @Override
            public boolean onCheckIsTextEditor() {
                Log.d(TAG, "onCheckIsTextEditor");
                return true;
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                // TODO Auto-generated method stub
                View view = Holder.this.getRootView();
                imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
                return false;
            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
            }



            class MyInputConnection extends BaseInputConnection {
                private SpannableStringBuilder myeditable;
                Holder mycustomview;

                public MyInputConnection(View targetView, boolean fullEditor) {
                    super(targetView, fullEditor);
                    mycustomview = (Holder) targetView;
                }

                public Editable getEditable() {
                    if (myeditable == null) {
                        myeditable = (SpannableStringBuilder) Editable.Factory.getInstance()
                        .newEditable("Placeholder");
                    }
                    return myeditable;
                }

                public boolean commitText(CharSequence text, int newCursorPosition) {
                    invalidate();
                    myeditable.append(text);
                    mycustomview.setText(text);
                    return true;
                }
            }


        }

Attached is the screen shot. Pic-1 is before touch, UI is some text. Pic-2 is when custom view is touched and text is received from soft keyboard. ![Pic-1, with initial text in custom view][1]

![Pic-2 when custom view is tapped][2]





  [1]: https://i.sstatic.net/rLYmR.png
  [2]: https://i.sstatic.net/gTnYY.png
Stepdame answered 5/1, 2015 at 18:34 Comment(0)
C
1

check this link o try this it may work:

imm.showSoftInput(view,0);
Carlisle answered 31/12, 2014 at 7:24 Comment(1)
after invoking keyborad how will i get the textGreenness
A
1

try this

InputMethodManager imm =    (InputMethodManager)     getSystemService(Context.INPUT_METHOD_SERVICE);
mSearchEditText.requestFocus();
imm.showSoftInput(mSearchEditText, InputMethodManager.SHOW_IMPLICIT);
Adur answered 31/12, 2014 at 7:44 Comment(0)
C
1

You can override onKeyUp(int keyCode, KeyEvent event) in your activity, and then send the key information (keyCode and/or event) to your view. Make sure you have a suitable custom method for handling key codes/events in your view (lets call it handleKeyEvent here).

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    yourCustomView.handleKeyEvent(keyCode, event);
}

source: https://groups.google.com/forum/#!topic/android-developers/0tQSZufLZTg

Cadmann answered 5/1, 2015 at 5:30 Comment(0)
N
0

Try this....

view.requestFocus();
view.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(InputEditText, InputMethodManager.SHOW_FORCED);

also check this

How to show soft-keyboard when edittext is focused

Nobles answered 31/12, 2014 at 7:49 Comment(3)
@M S Gadaj after invoking keyborad how will i get the textGreenness
it will open the keyboard when i double tap.thats fine after user will add some text right?how do i get the text ?Greenness
check this..#5420266Nobles
G
0

Use a textview itself. Seteditable(true) on a textview makes it behave like an edittext. So toggle between seteditable(true) and seteditable(false) as to when you want to edit and when you want to read

Grateful answered 7/1, 2015 at 10:37 Comment(0)
R
0

The accepted answer didn't work for me. To open the keyboard for a particular view, it must be focusable else keyboard won't show up.

android:focusable="true"
android:focusableInTouchMode="true"

This should do the trick.

Rubierubiginous answered 21/4, 2020 at 6:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.