0

How to get currently active KeyboardView in Android?

I've tried

KeyboardView keyboardView = new KeyboardView(context, null);
Keyboard keyboard = keyboardView.getKeyboard();

But it keeps on returning keyboard is Null.

I've tried this to get Keyboard height.

Ahmad Sanie
  • 3,678
  • 2
  • 21
  • 56
Knaus Irina
  • 789
  • 5
  • 15
  • 35
  • 1
    http://stackoverflow.com/questions/16788959/is-there-any-way-in-android-to-get-the-height-of-virtual-keyboard-of-device – Bojan Kseneman Apr 19 '15 at 07:40
  • 1
    here is a link that could help you to get the keyboard height: http://stackoverflow.com/questions/16788959/is-there-any-way-in-android-to-get-the-height-of-virtual-keyboard-of-device – Ahmad Sanie Apr 19 '15 at 07:45

2 Answers2

0

Actually, you can't get a reference of the current Keyboard.Detail is here(remember read the comments.).

Community
  • 1
  • 1
penkzhou
  • 1,200
  • 13
  • 30
0

I try get height of virtual keyboard Android like this [Is there any way in android to get the height of virtual keyboard of device

But I do not get heigth - only get heigth of screen.

(I make android plugin for Unity3d)

I try calc height in function Trace().

public class KeyboardTool
{

    private EditTextEx input;
    public Activity _activity;
    private PopupWindow window;
    private int height= 0;
    public KeyboardTool(Activity activity)
    {
        if (activity == null)
            activity = com.unity3d.player.UnityPlayer.currentActivity;

        _activity = activity;
        _activity.runOnUiThread(new Runnable() {@SuppressLint("NewApi") public void run() 
        {

            window = new PopupWindow(_activity);
            input = new EditTextEx(_activity);

            //window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
            //window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
            window.setTouchable(false);
            window.setFocusable(true);
            window.setSplitTouchEnabled(false);
            window.setOutsideTouchable(true);
            window.setIgnoreCheekPress();
            window.setClippingEnabled(false);
            window.setTouchInterceptor(new OnTouchListener()
            {

                @Override
                public boolean onTouch(View arg0, final MotionEvent arg1) {
                    Log.i("touch", arg0.toString() + ":" + arg1.toString());

                    _activity.dispatchTouchEvent(arg1);

                    return true;
                }

            });
            //window.
            window.setContentView(input);
            window.setBackgroundDrawable(new ColorDrawable(0));
            //window.setBackgroundDrawable(null);
            //


//          _activity.addContentView(input, new LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT));
//          _activity.addContentView( new EditTextEx(_activity), new LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT));
//          _activity.onWindowFocusChanged(false);
            input.setInputType(InputType.TYPE_CLASS_TEXT);
            input.setBackgroundColor(0);
            input.setVisibility(View.VISIBLE);
            input.setFocusable(true);
            input.setFocusableInTouchMode(true);

            window.setInputMethodMode(PopupWindow.INPUT_METHOD_FROM_FOCUSABLE);
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            //window.showAtLocation(_activity.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, 100);
            //input.requestFocus();
            //input.bringToFront();

//          UnityPlayer.currentActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//          window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

            Trace();
        }});
    }

    @SuppressLint("NewApi") private void Trace()
    {
        Window rootWindow = UnityPlayer.currentActivity.getWindow();
        View rootView = rootWindow.getDecorView().findViewById(android.R.id.content);

        Log.i("xxx", "xxx rootView " + rootView);

        rootView.getViewTreeObserver().addOnGlobalLayoutListener
        (
                new ViewTreeObserver.OnGlobalLayoutListener() {
                public void onGlobalLayout(){
                    Rect r = new Rect();
                    View view = UnityPlayer.currentActivity.getWindow().getDecorView();
                    view.getWindowVisibleDisplayFrame(r);
                    Log.i("xxx", "xxx left " + r.left +" " + r.top+ " " +  r.right + " " + r.bottom);
                    // r.left, r.top, r.right, r.bottom

                    Window rootWindow = UnityPlayer.currentActivity.getWindow();
                    View rootView = rootWindow.getDecorView().findViewById(android.R.id.content);
                    int heightDiff = rootView.getHeight()- rootWindow.getDecorView().getHeight();
                    Log.i("", "xxx heightDiff " + heightDiff + "rootView.getHeight() " + rootView.getHeight() + " rootWindow.getDecorView().getHeight() " + rootWindow.getDecorView().getHeight());
                }
                });

        View keyboardView22 = rootWindow.getDecorView().findViewById(android.R.id.keyboardView);
        Log.i("", "xxxx keyboardView22 " + keyboardView22);

        View view = UnityPlayer.currentActivity.getCurrentFocus();
        Log.i("xxx", "xxx view current " + view);
        Log.i("xxx", "xxx view as " + view.getHeight() + " " + view.getBottom() + " "+ view.getMeasuredHeight());
        KeyboardView keyboardView2 = new KeyboardView(UnityPlayer.currentActivity.getApplicationContext(),   null);
        Log.i("", "xxx keyboardView2.getKeyboard() "  + keyboardView2.getKeyboard() + " " + keyboardView2.getHeight());
        if (keyboardView2.getKeyboard() != null)
        {
            int height = (keyboardView2.getKeyboard()).getHeight();
            Log.i("", "xxx Trace  height2 + " + height + " "  + Toast.LENGTH_LONG);
        }

        Log.i("", "xxx input " + input.getBottom() + " " + input.findViewById(android.R.id.keyboardView));

        Rect outRect = new Rect();
        input.getWindowVisibleDisplayFrame(outRect);
        Log.i("", "xxxx outRect " + outRect.height() + " " + outRect.width() + " " + outRect.left + " " + outRect.right + " "+ outRect.centerY());
        if (outRect.centerY() != 0)
            height = outRect.centerY();
    }

    private boolean done;
    public boolean isDone()
    {
        UpdateStates();
        return done;
    }
    private boolean canceled;
    public boolean isCanceled()
    {
        UpdateStates();
        return canceled;
    }

    public void UpdateStates()
    {
        _activity.runOnUiThread(new Runnable() {public void run() 
        {
            done = input.done;
            canceled = input.canceled;
        }});
    }

    public void Show()
    {
        this.canceled = false;
        this.done = false;
        if (input != null)
        {
            input.done = false;
            input.canceled = false;
        }
        _activity.runOnUiThread(new Runnable() {public void run() 
        {
            window.setFocusable(true);
            input.setFocusable(true);
            input.setFocusableInTouchMode(true);

            window.showAtLocation(_activity.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, 0);
            input.requestFocus();
            input.bringToFront();
            InputMethodManager imm = (InputMethodManager)_activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(input, 0);

            Trace();
        }});
    }

    public void Hide()
    {
        _activity.runOnUiThread(new Runnable() {public void run() 
        {
            window.setFocusable(false);
            input.setFocusable(false);
            input.setFocusableInTouchMode(false);
            input.clearFocus();
            window.dismiss();

            InputMethodManager imm = (InputMethodManager)_activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(input.getWindowToken(), 0);

            Trace();
        }});
    }
    private String text;
    public String GetText()
    {
        _activity.runOnUiThread(new Runnable() {public void run() 
        {
            text = input.getText().toString();
        }});
        return text;
    }

    public void SetText(final String text)
    {
        _activity.runOnUiThread(new Runnable() {public void run() 
        {
        input.setText(text);
        input.setSelection(text.length());
        }});
    }

    public void SetRect (final int x, final int y, final int width, final int height)
    {   
        _activity.runOnUiThread(new Runnable() {public void run() 
        {

            window.update(x, y, width, height);
            window.setWidth(width);
            window.setHeight(height);
            input.setWidth(width);
            input.setHeight(height);
        }});
    }

    public int GetHeight()
    {
        return this.height;
    }
}
Community
  • 1
  • 1
Knaus Irina
  • 789
  • 5
  • 15
  • 35