Show/Hide soft keyboard from native

Put problems with the Android NDK in this forum.

Show/Hide soft keyboard from native

Postby timoshenko_av » Thu Dec 09, 2010 3:23 pm

Now I use JAVA code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. InputMethodManager mgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  2. mgr.showSoftInput(mGLView,0);
  3.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

which called from C++ via JNI to show users soft keyboard,but I don`t know how to handle hardware back key which hide keyboard. Can anyone help me to solve this problem!
Much thanks!
timoshenko_av
Freshman
Freshman
 
Posts: 8
Joined: Tue Oct 05, 2010 10:02 am

Top

Re: Show/Hide soft keyboard from native

Postby Kcm » Mon Jan 10, 2011 11:22 am

I use this in Java :

public void openKeyboard(String msg)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText mTextInputWidget = new EditText(this);
mTextInputWidget.setHint("");
builder.setTitle(msg);
builder.setPositiveButton("done", null);
builder.setNegativeButton("cancel", null);

builder.setView(mTextInputWidget);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
nativeSendTextValue(mTextInputWidget.getText().toString().toLowerCase());
}
});

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}

///////////////////////////////////////////////////////////////////////////////////////////////

@Override public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
//close keyboard here
}
}

In C :
----------------------------------------

void ShowKeyboard(const char * msg)
{
int ver = _env->GetVersion();
jclass cls = _env->GetObjectClass(_thiz);
jmethodID mid = _env->GetMethodID(cls, "ShowKeyboard", "(Ljava/lang/String;)V" ); //"(I)V");

_env->CallVoidMethod(_thiz, mid, _env->NewStringUTF(msg));
}

extern "C" void Java_xxx_xxx_xxx_JNICall_nativeSendTextValue(JNIEnv * env, jobject thiz, jstring myString)
{
valueString = env->GetStringUTFChars(myString, NULL);

if (valueString == NULL)
{
valueString = "";
}

NdkLog("SendTextValue up= '%s'", valueString);
}

i hope this will help...
Kcm
Junior Developer
Junior Developer
 
Posts: 11
Joined: Fri Oct 15, 2010 9:43 am

Re: Show/Hide soft keyboard from native

Postby ratamovic » Sat May 14, 2011 10:06 am

I am also interested in this question. I am trying to show keyboard directly from NDK using Gingerbread. I try the following:
Code: Select all
        ANativeActivity_showSoftInput(mApplication->activity,
            ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED);

or
Code: Select all
        ANativeActivity_showSoftInput(mApplication->activity,
            ANATIVEACTIVITY_SHOW_SOFT_INPUT_IMPLICIT);


but nothing works! Have anyone tried that?
Many thanks
http://www.codexperiments.com/
Because IT is an experimental science
ratamovic
Freshman
Freshman
 
Posts: 6
Joined: Fri Jan 23, 2009 3:20 am

Re: Show/Hide soft keyboard from native

Postby ratamovic » Sat May 14, 2011 8:27 pm

For those who are interested, I wrote a JNI method to do the same thing :? :
Code: Select all
android_app* mApplication;

...

void displayKeyboard(bool pShow) {
    // Attaches the current thread to the JVM.
    jint lResult;
    jint lFlags = 0;

    JavaVM* lJavaVM = mApplication->activity->vm;
    JNIEnv* lJNIEnv = mApplication->activity->env;

    JavaVMAttachArgs lJavaVMAttachArgs;
    lJavaVMAttachArgs.version = JNI_VERSION_1_6;
    lJavaVMAttachArgs.name = "NativeThread";
    lJavaVMAttachArgs.group = NULL;

    lResult=lJavaVM->AttachCurrentThread(&lJNIEnv, &lJavaVMAttachArgs);
    if (lResult == JNI_ERR) {
        return;
    }

    // Retrieves NativeActivity.
    jobject lNativeActivity = mApplication->activity->clazz;
    jclass ClassNativeActivity = lJNIEnv->GetObjectClass(lNativeActivity);

    // Retrieves Context.INPUT_METHOD_SERVICE.
    jclass ClassContext = lJNIEnv->FindClass("android/content/Context");
    jfieldID FieldINPUT_METHOD_SERVICE =
        lJNIEnv->GetStaticFieldID(ClassContext,
            "INPUT_METHOD_SERVICE", "Ljava/lang/String;");
    jobject INPUT_METHOD_SERVICE =
        lJNIEnv->GetStaticObjectField(ClassContext,
            FieldINPUT_METHOD_SERVICE);
    jniCheck(INPUT_METHOD_SERVICE);

    // Runs getSystemService(Context.INPUT_METHOD_SERVICE).
    jclass ClassInputMethodManager = lJNIEnv->FindClass(
        "android/view/inputmethod/InputMethodManager");
    jmethodID MethodGetSystemService = lJNIEnv->GetMethodID(
        ClassNativeActivity, "getSystemService",
        "(Ljava/lang/String;)Ljava/lang/Object;");
    jobject lInputMethodManager = lJNIEnv->CallObjectMethod(
        lNativeActivity, MethodGetSystemService,
        INPUT_METHOD_SERVICE);

    // Runs getWindow().getDecorView().
    jmethodID MethodGetWindow = lJNIEnv->GetMethodID(
        ClassNativeActivity, "getWindow",
        "()Landroid/view/Window;");
    jobject lWindow = lJNIEnv->CallObjectMethod(lNativeActivity,
        MethodGetWindow);
    jclass ClassWindow = lJNIEnv->FindClass(
        "android/view/Window");
    jmethodID MethodGetDecorView = lJNIEnv->GetMethodID(
        ClassWindow, "getDecorView", "()Landroid/view/View;");
    jobject lDecorView = lJNIEnv->CallObjectMethod(lWindow,
        MethodGetDecorView);

    if (pShow) {
        // Runs lInputMethodManager.showSoftInput(...).
        jmethodID MethodShowSoftInput = lJNIEnv->GetMethodID(
            ClassInputMethodManager, "showSoftInput",
            "(Landroid/view/View;I)Z");
        jboolean lResult = lJNIEnv->CallBooleanMethod(
            lInputMethodManager, MethodShowSoftInput,
            lDecorView, lFlags);
    } else {
        // Runs lWindow.getViewToken()
        jclass ClassView = lJNIEnv->FindClass(
            "android/view/View");
        jmethodID MethodGetWindowToken = lJNIEnv->GetMethodID(
            ClassView, "getWindowToken", "()Landroid/os/IBinder;");
        jobject lBinder = lJNIEnv->CallObjectMethod(lDecorView,
            MethodGetWindowToken);

        // lInputMethodManager.hideSoftInput(...).
        jmethodID MethodHideSoftInput = lJNIEnv->GetMethodID(
            ClassInputMethodManager, "hideSoftInputFromWindow",
            "(Landroid/os/IBinder;I)Z");
        jboolean lRes = lJNIEnv->CallBooleanMethod(
            lInputMethodManager, MethodHideSoftInput,
            lBinder, lFlags);
    }

    // Finished with the JVM.
    lJavaVM->DetachCurrentThread();
}
http://www.codexperiments.com/
Because IT is an experimental science
ratamovic
Freshman
Freshman
 
Posts: 6
Joined: Fri Jan 23, 2009 3:20 am

Top

Return to NDK Problems

Who is online

Users browsing this forum: No registered users and 4 guests