How to save a sliding box from the top of the virtual keyboard

I've been scratching my head all day. according to one of my actions (and only one), when I call the virtual keyboard, a sliding drawer handle appears above it. I managed to fix this problem in all other actions in my application by setting android: windowSoftInputMode = "adjustPan" in each action in the Manafest.xml file, including the activity in question. also as far as I was able to determine that none of the objects in the activity has focus (if I cannot figure out how to find it). I checked the focus using this.getCurrentFocus () and then doing view.clearFocus () in the returned view, if any. until he returned the idea, as far as I can say, nothing has focus.

any ideas?

+5
source share
1 answer

This is my workaround .. in the Office that contains the EditText I have a subclass that extends EditText. In this subclass, I override the method onMeasure()that checks if the keyboard is open.

    public static class MyEditText extends EditText {
            MyActivity context;

            public void setContext(MyActivity context) {
                this.context = context;
            }

            @Override
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                int height = MeasureSpec.getSize(heightMeasureSpec);
                Activity activity = (Activity)getContext();
                Rect rect = new Rect();
                activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
                int statusBarHeight = rect.top;
                int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
                int diff = (screenHeight - statusBarHeight) - height;
                // assume all soft keyboards are at least 128 pixels high
                if (diff>128)
                     context.showHandle(false);
                else
                     context.showHandle(true);

                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
    }

Then in action, if the keyboard is open, the drawer handle is set to a transparent image 1px x 1px, if the keyboard is hidden, a real handle is displayed:

private void showHandle(boolean show) {
    ImageView drawer_handle = (ImageView) findViewById(R.drawable.handle);
    if (show)
            drawer_handle.setImageResource(R.drawable.handle);
    else
            drawer_handle.setImageResource(R.drawable.handle_blank);
}

Finally, make sure you call setContext()in onCreate()fromMyActivity

MyEditText met = (MyEditText) findViewById(R.id.edit_text);
met.setContext(this);
0
source

All Articles