Call android keyboard doesn't work

RazRaz
edited August 2016 in Android Mode

hi, I've tried to run keyboard on android and I get an error. the error is: the function getSystemService does not exist. here is the code.

import android.view.inputmethod.InputMethodManager;
import android.content.Context;
boolean showing=false;

void setup() {
  fullScreen();
}

void draw() {
  if (showing)
    showVirtualKeyboard();
  else
    hideVirtualKeyboard();
}

void mousePressed() {
  showing=true;
}

void showVirtualKeyboard() {
  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}

void hideVirtualKeyboard() {
  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}

anybody knows why?

Answers

  • I would suggest using the ketai library. You can do exactly those calls and the library will handle everything to show/hide your keyboard:

    ketai.org/reference/ui/ketaikeyboard/

    An example using the library is found here:

    http://ketai.org/examples/ui/

    You need to install the library. In processing, go to menu sketch >> import library... >> Add library... and then search for ketai and install it.

    I hope this helps,

    Kf

  • @raz===

    Not time enough to test your code which seems good except that you are not in an activity but in a fragment: getSystemService comes from Context which is extended by an Activity, not in a fragment; so you have to change your code adding reference to the main activity which has created the fragment...

    Try that::

    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

  • @akenaton not working for me. now it shows "the function getActivity() does not exist".

  • Check this example: editText, akenaton's post.

    Are you trying getActivity or this.getActivity() ?

    Notice that example also activates the keyboard. I hope this helps,

    Kf

  • @raz=== yes, i know that it complains about getActivity() yet it works ( of course if you have imported:: import android.app.Activity;

  • @akenaton I've imported this abd it's still not working

  • edited August 2016

    @Raz=== as soon as i get 5 ' i ll try && tell you...But it must work!

  • @Raz===

    try this:: it works; though there is another problem with your boolean that you have to solve.

            import android.view.inputmethod.InputMethodManager;
            import android.content.Context;
            import android.app.Activity;
            boolean showing = false;
            Activity act;
    
            void setup() {
              fullScreen();
              act = this.getActivity();
            }
    
            void draw() {
    
    
              if (showing)
                showVirtualKeyboard();
              else
                hideVirtualKeyboard();
            }
    
            void mousePressed() {
              showing=true;
    
            }
    
            void showVirtualKeyboard() {
              InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            }
    
            void hideVirtualKeyboard() {
              InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
            }
    
  • edited August 2016

    @raz===

    try this (which solves your boolean and toggle in draw)

     import android.view.inputmethod.InputMethodManager;
            import android.content.Context;
            import android.app.Activity;
            boolean showing= false;
            Activity act;
    
    
    
            void settings(){
              fullScreen();
            }
            void setup() {
    
              act = this.getActivity();
    
    
            }
    
            void draw() {
    
    
            }
    
            void mouseReleased() {
    
              if (!showing){
                showVirtualKeyboard();
              }else{
                hideVirtualKeyboard();
              }
    
    
            }
    
            void showVirtualKeyboard() {
              InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
              showing = true;
            }
    
            void hideVirtualKeyboard() {
              InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
    
              imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    
              showing = false;
    
            }
    
Sign In or Register to comment.