Virtual Keyboard doesn't work

Hi all!

I’m using Android Processing and I’m trying to use the virtual keyboard to type things into a text field from cp5 for a search bar. When I tap the text field, the keyboard shows up like I want it to, and if I manage to type the right word without it glitching, the search function still works. However, when I type things in, a line appears between each letter, sometimes the letters overlap, and i, l, j, and k do not even work. It also does not let me delete characters (backspace). Here’s what it looks like. I typed random letters to show the overlap.

The lines in between the letters and deleting was not an issue when I ran it on my computer.

I used the following code for the virtual keyboard which I just found in a discussion on the old Processing forum:

void mousePressed(){
  if(overTextfield())  //make keyboard appear when search bar is pressed
    showSoftKeyboard();
}
 
void showSoftKeyboard() {
  android.view.inputmethod.InputMethodManager imm = (android.view.inputmethod.InputMethodManager) getActivity().getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
  imm.showSoftInput(getActivity().getCurrentFocus(), 0);
}
 
void hideSoftKeyboard() {
  android.view.inputmethod.InputMethodManager imm = (android.view.inputmethod.InputMethodManager) getActivity().getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
  imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}

Would anyone be able to help me fix the keyboard?
Thanks!

I don’t think the problem is in the code you provided by more likely due to the implementation of the controlP5. I am guessing about this, and there is still a small chance it could be a problem in Android. The best way to address this problem is to generate a small code base that reproduces the problem. If you post this code here, somebody could try to reproduce it and confirm it.

To solve this problem, instead of using controlP5, you could use Android’s native widgets directly in your sketch. You can use this example to get you started.

Kf

1 Like

@kat===

@kfrajer is probably right= that is a problem with controlP5; in order to avoid it you have only to create some native editText from Android : as there was here a recent post about keyboard i have verified that the code i have put in the previous forum works fine.

1 Like

Thank you for your response and sorry for a late reply. I tried copying the code from @akenaton and changed the problem with @Override and imported EditText which you mentioned but it won’t run on my tablet.

This is the error:

FATAL EXCEPTION: main
Process: processing.test.keyboardtest, PID: 31610
java.lang.RuntimeException: Unable to start activity ComponentInfo{processing.test.keyboardtest/processing.test.keyboardtest.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.view.ViewGroup.addView(android.view.View)’ on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.view.ViewGroup.addView(android.view.View)’ on a null object reference
at processing.test.keyboardtest.KeyboardTest.onStart(KeyboardTest.java:71)
at processing.android.PFragment.onStart(PFragment.java:168)
at android.support.v4.app.Fragment.performStart(Fragment.java:2369)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1458)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3217)
at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3176)
at android.support.v4.app.FragmentController.dispatchStart(FragmentController.java:203)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:562)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1256)
at android.app.Activity.performStart(Activity.java:6972)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2937)
… 9 more

Would you know why this happens?

That is a first lead for your problem. However, without seeing your code we are going to be shooting in the dark, and that is not fun.

Kf

Hehe sorry, I copied and pasted the code from the thread that you sent the link of:

import android.app.Activity;
 import android.content.Context;
 import android.widget.FrameLayout;
 import android.widget.RelativeLayout;
 import android.text.Editable;
 import android.graphics.Color;
 import android.widget.Toast;
 import android.os.Looper;
 import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.os.Bundle;
import android.widget.EditText;
 
 
 EditText edit;
 Activity act;
 Context mC;
 FrameLayout fl;
 
    @Override 
    public void onStart() {
        super.onStart();
 
        act = this.getActivity();
        mC= act.getApplicationContext();
        //show or hide the keyboard at start
         act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        edit = new EditText(mC);
        edit.setLayoutParams(new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT,
                                   RelativeLayout.LayoutParams.MATCH_PARENT
                                   ));
                                   edit.setHint("   YOU CAN WRITE!");
 
            edit.setTextColor(Color.rgb(200,0,0));
            edit.setBackgroundColor(Color.WHITE);
            edit.requestFocus();
 
 
        fl = (FrameLayout)act.findViewById(0x1000);
  fl.addView(edit);
 

    }
 
 void setup(){
   size(600,900);
   orientation(LANDSCAPE);
   background(255,0,0);
 
   Looper.prepare();
 
 }
 
 void draw(){
 
 
 
 
   String txt = edit.getText().toString();//i you want to save it or transform it
   println(txt);
 
 }
 
 public void onResume(){
 
   super.onResume();
   //show the softinput keyboard at start
   act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
 }

For right now, I’m just using this to see how the Android EditText widget actually works because I have no idea how to use it. I’m trying to replace the textfield that I got from control P5. Thanks again!

Consider the changes below. I think that should fix it. Can you confirm what is your Android mode version and Processing version? Also what is your OS. I am assuming you have run other simpler examples in your phone before, right? Always try simple examples just for sanity check…

Kf


import    android.R;

...

//fl = (FrameLayout)act.findViewById(0x1000);  //Valid for AM version older than 4
fl = (FrameLayout)act.findViewById(R.id.content);  //Use for AM 4.0 and newer
1 Like

@kat===
the error is probably what kf has pointed out: since one year the frameLayout used by P5 has an id generated randomly, so to get it you have to use another way; as for me i use:

fl = (FrameLayout)act.getWindow().getDecorView().getRootView();

2 Likes

Thanks! @kfrajer @akenaton

@akenaton’s worked for me. I was trying to get that to work so I could try to figure out how the android widgets work but I’m still pretty clueless. I’m trying to replace the cp5 textfield that I had with the android one but I don’t know how to use any of the libraries that android has. Does anyone know where I can find more info about the android libraries? I’m very lost as to how I can implement the native widgets.

Thanks!