Android How to adjust layout in Full Screen Mode when softkeyboard is visible

前端 未结 25 2156
后悔当初
后悔当初 2020-11-22 03:56

I have researched a lot to adjust the layout when softkeyboard is active and I have successfully implemented it but the problem comes when I use android:theme=\"@andro

25条回答
  •  无人共我
    2020-11-22 04:40

    I implemented Joseph Johnson solution and it worked well, I noticed after using this solution sometimes the drawer on the application will not close properly. I added a functionality to remove the listener removeOnGlobalLayoutListener when the user closes the fragment where are edittexts located.

        //when the application uses full screen theme and the keyboard is shown the content not scrollable! 
    //with this util it will be scrollable once again
    //http://stackoverflow.com/questions/7417123/android-how-to-adjust-layout-in-full-screen-mode-when-softkeyboard-is-visible
    public class AndroidBug5497Workaround {
    
    
        private static AndroidBug5497Workaround mInstance = null;
        private View mChildOfContent;
        private int usableHeightPrevious;
        private FrameLayout.LayoutParams frameLayoutParams;
        private ViewTreeObserver.OnGlobalLayoutListener _globalListener;
    
        // For more information, see https://code.google.com/p/android/issues/detail?id=5497
        // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
    
        public static AndroidBug5497Workaround getInstance (Activity activity) {
            if(mInstance==null)
            {
                synchronized (AndroidBug5497Workaround.class)
                {
                    mInstance = new AndroidBug5497Workaround(activity);
                }
            }
            return mInstance;
        }
    
        private AndroidBug5497Workaround(Activity activity) {
            FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
            mChildOfContent = content.getChildAt(0);
            frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    
            _globalListener = new ViewTreeObserver.OnGlobalLayoutListener()
            {
    
                @Override
                public void onGlobalLayout()
                {
                     possiblyResizeChildOfContent();
                }
            };
        }
    
        public void setListener()
        {
             mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(_globalListener);
        }
    
        public void removeListener()
        {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                mChildOfContent.getViewTreeObserver().removeOnGlobalLayoutListener(_globalListener);
            } else {
                mChildOfContent.getViewTreeObserver().removeGlobalOnLayoutListener(_globalListener);
            }
        }
    
        private void possiblyResizeChildOfContent() {
            int usableHeightNow = computeUsableHeight();
            if (usableHeightNow != usableHeightPrevious) {
                int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
                int heightDifference = usableHeightSansKeyboard - usableHeightNow;
                if (heightDifference > (usableHeightSansKeyboard/4)) {
                    // keyboard probably just became visible
                    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
                } else {
                    // keyboard probably just became hidden
                    frameLayoutParams.height = usableHeightSansKeyboard;
                }
                mChildOfContent.requestLayout();
                usableHeightPrevious = usableHeightNow;
            }
        }
    
        private int computeUsableHeight() {
            Rect r = new Rect();
            mChildOfContent.getWindowVisibleDisplayFrame(r);
            return (r.bottom - r.top);
        } 
    }
    

    uses the class where is my edittexts located

    @Override
    public void onStart()
    {
        super.onStart();
        AndroidBug5497Workaround.getInstance(getActivity()).setListener();
    }
    
    @Override
    public void onStop()
    {
        super.onStop();
        AndroidBug5497Workaround.getInstance(getActivity()).removeListener();
    }
    

提交回复
热议问题