Android Keybaord


#1

In 2014 I needed to get use the keyboard on Android. At the time I found this post

Unfortunately it worked for an older OF and and android than I was using at the time.

I see these methods in OFAndroid.java

public static native void onKeyDown(int keyCode);
public static native void onKeyUp(int keyCode);

How can I show and hide the keyboard?


#2

Hi @fresla,

Is this urgent or do you have time? If urgent, I can give you some code. If you have some time, I can help you with an addon for that.


#3

Oh, and do you need an EditText or only the keyboard? I have some code for EditText with a keyboard. The common way on Android is using an EditText.


#4

this patch, it is very old and poorly constructed…
if we want to pack a addons let me know if you need help…


#5

@Rancs
Thanks for the reply (I have been reading a lot of your other posts, you are really doing some great work for android and OF, thanks!), I need it for email entry so edittext would be great, but from memory I think that drawing an editText inside the OF view is a pain, but I would like to try. As for time, I have a few more days to sort this out, not sure if I can dedicate the time to creating an addon as the project will have to walk out the door and then the next job will start… I did get an OF derived text box and validation code for email address working that I can test with pseudo code at the moment.

I did find this

https://github.com/lumoslabs/ofxGeneric

I will check it out later today and see if it works.

If you have something for EditText I would love to try it as well, it will be a bit better solution that what I have been able to gather so far.

Cheers

Fred


#6

@Rancs
Hey if you have a chance can you post the code for the editText, I tried to have my way with the code I posted above but I cannot get it working.


#7

I created an android-study project on git, with a draft jni call to query the native Android keyboard, but I think it does not work well because of this line that you have to change:

InputMethodManager inputMgr = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);

code:

when I have time I return to


#8

Hi @fresla,

Is the code @kashim shared what you are looking for? If so, that’s great. If not, please let me know and I can convert my EditText code into an ofxaddon for you.


#9

@kashim Hey, I implemented your methods and even wit the change above I cannot get it to work. Everything compiles but when I try to run the method I get this error

05-18 09:32:57.456 30789-30789/cc.openframeworks.androidCameraExample A/art: art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: expected a constructor but void cc.openframeworks.androidCameraExample.OFActivity.ofkeyboard(): 0xb0636ad0

Yes, I started with the androidCameraExample.

I just implemented this method in the OFActivity java

 public void ofkeyboard(){

        System.out.println("********open ofkeyboard*********");
        try{
            InputMethodManager inputMgr =  (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            System.out.println("********open ofkeyboard*********");
        }catch(Exception e){
            System.out.println("catch:"+e);
        }
    }

And this is the cpp file

void ofApp::ofAndroidKeyboard(){
	ofLog()<<"start keyboard show";
	jclass javaClass = ofGetJNIEnv()->FindClass("cc/openframeworks/androidCameraExample/OFActivity");
	if(javaClass==0)
	{
		ofLogError("ofAppAndroidWindow") << "ofkeyboard(): couldn't find OFActivity java class";
		return;
	}
	jmethodID method = ofGetJNIEnv()->GetMethodID(javaClass, "ofkeyboard","()V");
	if(!method){
		ofLogError("ofAppAndroidWindow") << "ofkeyboard(): couldn't find OFActivity ofkeyboard method";
		return;
	}
	else{
		ofGetJNIEnv()->NewObject(javaClass,method);
	}
}

and this in the .h

void ofAndroidKeyboard();

Is there something I missed in your example?

Fred


#10

try to change this call:

with:

InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMrg.showSoftInput(EDITABLE_VIEW, InputMethodManager.SHOW_IMPLICIT);

just out of curiosity, what release of Android you are using?


#11

Hi, I am running the 0.9.3 OF for android and I did not change the deployment targets so the device is running lollipop (nexus 5) and my gradle settings are unchanged

defaultConfig {
minSdkVersion 8
targetSdkVersion 22
}

In your above change EDITABLE_VIEW cannot be resolved, I am guessing this is the view that OF uses to draw to? How can I get access to this or define it in the java file?

Thanks again for the help.

Just in case I tried a bunch of stuff in between to get it to work, the error is called on the JNI call

env->NewObject(javaClass,method);

I tried to switch it to CallVoidMethod as that seemed logical but I cannot call it from there…


#12

ehm… at this moment I do not have a development environment Android / openFrameworks

However, tonight I go back to Italy in my laboratory where I can do tests for this problem…

I will try to do some tests, I keep you updated.


#13

Hey no problems, I will keep trying. Thanks so much again for your time and help.


#14

Hi @fresla, I will send you something me too. Just give me a day please. All the best.


#15

That would be amazing, thanks a lot for the assistance.

Fred


#16

Hey @fresla, I wrote some simple thing to use Android EditText with a keyboard. Can you try it:

https://github.com/noyanc/ofxAndroidExtendedEditText


#17

@Rancs

Wow, thanks, it looks great and it is a cool structure. However it does not run for me, everything builds fine but then it crashes with this error:

05-19 18:28:57.073 26580-26580/cc.openframeworks.androidEmptyExample E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                               Process: cc.openframeworks.androidEmptyExample, PID: 26580
                                                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference
                                                       at cc.openframeworks.ofxaddons.ofxandroidextendededittext.OFXAndroidExtendedEditText$2.run(OFXAndroidExtendedEditText.java:50)

Cheers

Fred


#18

Hey @fresla,

Unfortunately I am not in the office now.

In the ofxAndroidExtendedEditText.cpp, can you comment out the line rlayout = (RelativeLayout) activity.findViewById… and write this line instead:

rlayout = (RelativeLayout) this.getParent();

Please try that, let me know if it works. I don’t have access to a computer.


#19

Cheers this does not work though

cannot resolve method 'getParent()'


#20

Ooops sorry the later I suggested is definitely wrong. I don’t know why the initial doesn’t work. I will check it tomorrow morning in the office.