Search code examples
androidtextviewcustom-keyboard

Custom keyboard unable to write into TextView


I'm new to Android studio, curently I'm trying to build a custom keyboard within an app(Not Service Keyboard), I manage to done it by referring online resources, but when I try to switched from editText to TextView, my keyboard input just unable to write into the TextView, is there a different method for TextView?

I tried something like textView.setText(InputType.TYPE_CLASS_TEXT); The app just forced stop in this situation.

This is the editText that works fine.

EditText editText = (EditText) findViewById(R.id.editText);  
myKeyboard keyboard = (myKeyboard) findViewById(R.id.keyboard);

editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);

InputConnection inputConnection = editText.onCreateInputConnection(newEditorInfo());
keyboard.setInputConnection(inputConnection);

Code below TextView has no respond while pressing on my keyboard.

TextView textView = (TextView) findViewById(R.id.textView);  
myKeyboard keyboard = (myKeyboard) findViewById(R.id.keyboard);

textView.setRawInputType(InputType.TYPE_CLASS_TEXT);
textView.setTextIsSelectable(true);

This is the code that I have tried but app forced stopped.

TextView textView = (TextView) findViewById(R.id.textView);
myKeyboard keyboard = (myKeyboard) findViewById(R.id.keyboard);

textView.setText(InputType.TYPE_CLASS_TEXT);

For your information, the online resource that I referred to is at How to make an Android custom keyboard? Thanks in advance.


Solution

  • This is some alternate way to achieve the result. Add a TextWatcher to your editText, and sync it with your TextView. Code shows as below:-

            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    TextView textView = findViewById(R.id.textView);
                    textView.setText(s);
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                }
            });