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

前端 未结 25 1776
后悔当初
后悔当初 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:46

    Don't use:

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    

    because works bad. Instead of that, use:

    fun setFullScreen(fullScreen: Boolean) {
            val decorView = getWindow().getDecorView()
            val uiOptions : Int
            if(fullScreen){
                uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN // this hide statusBar
                toolbar.visibility = View.GONE // if you use toolbar
                tabs.visibility = View.GONE // if you use tabLayout
            } else {
                uiOptions = View.SYSTEM_UI_FLAG_VISIBLE // this show statusBar
                toolbar.visibility = View.VISIBLE
                tabs.visibility = View.VISIBLE
            }
            decorView.setSystemUiVisibility(uiOptions)
        }
    
    0 讨论(0)
  • 2020-11-22 04:46

    In my case, this issue started happening once I added Crosswalk to my Cordova application. My app is not used in fullscreen and android:windowSoftInputMode="adjustPan".

    I already had the ionic keyboard plugin in the application, so detecting if the keyboard was up or down was easy thanks to it:

    // Listen for events to when the keyboard is opened and closed
    window.addEventListener("native.keyboardshow", keyboardUp, false);
    window.addEventListener('native.keyboardhide', keyboardDown, false);
    
    function keyboardUp()
    {
        $('html').addClass('keyboardUp');
    }
    
    function keyboardDown()
    {
        $('html').removeClass('keyboardUp');
    }
    

    I tried all of the fixes above but the simple line that ended up doing it for me was this bit of css:

    &.keyboardUp {
            overflow-y: scroll;
    }
    

    Hope this saves you the few days I spent on this. :)

    0 讨论(0)
  • 2020-11-22 04:47

    Add android:fitsSystemWindows="true" to the layout, and this layout will resize.

    0 讨论(0)
  • 2020-11-22 04:52

    I have tried out all the possible answers from stackOverflow, finally i solved after a week Long search . I have used the coordinate layout and i changed this with linearLayout and my problem is fixed. I dont know possibly the coordinate layout has bugs or anything my mistake.

    0 讨论(0)
  • 2020-11-22 04:53

    I tried the solution from Joseph Johnson, but like others I ran into the gap-between-content-and-keyboard problem. The problem occurs because the soft input mode is always pan when using full-screen mode. This panning interferes with Joseph's solution when you activate an input field that would be hidden by the soft input.

    When the soft input appears, the content is first panned based on its original height, and then resized by the layout requested by the Joseph's solution. The resizing and subsequent layout do not undo the panning, which results in the gap. The full order of events is:

    1. Global layout listener
    2. Panning
    3. Layout of content (= actual resizing of content)

    It is not possible to disable panning, but it is possible to force the pan offset to be 0 by changing the height of the content. This can be done in the listener, because it is run before panning takes place. Setting the content height to the available height results in a smooth user experience, i.e. no flickering.

    I also made these changes. If any of these introduce issues, let me know:

    • Switched determination of available height to use getWindowVisibleDisplayFrame. The Rect is cached to prevent a little bit of unneeded garbage.
    • Allow the listener to be removed too. This is useful when you reuse an activity for different fragments having different full-screen requirements.
    • Do not distinguish between keyboard shown or hidden, but always set the content height to the visible display frame height.

    It has been tested on a Nexus 5, and emulators running API levels 16-24 with screen sizes ranging from tiny to big.

    The code has been ported to Kotlin, but porting my changes back to Java is simple. Let me know if you need help:

    class AndroidBug5497Workaround constructor(activity: Activity) {
        private val contentContainer = activity.findViewById(android.R.id.content) as ViewGroup
        private val rootView = contentContainer.getChildAt(0)
        private val rootViewLayout = rootView.layoutParams as FrameLayout.LayoutParams
        private val viewTreeObserver = rootView.viewTreeObserver
        private val listener = ViewTreeObserver.OnGlobalLayoutListener { possiblyResizeChildOfContent() }
    
        private val contentAreaOfWindowBounds = Rect()
        private var usableHeightPrevious = 0
    
        // I call this in "onResume()" of my fragment
        fun addListener() {
            viewTreeObserver.addOnGlobalLayoutListener(listener)
        }
    
        // I call this in "onPause()" of my fragment
        fun removeListener() {
            viewTreeObserver.removeOnGlobalLayoutListener(listener)
        }
    
        private fun possiblyResizeChildOfContent() {
            contentContainer.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds)
            val usableHeightNow = contentAreaOfWindowBounds.height()
            if (usableHeightNow != usableHeightPrevious) {
                rootViewLayout.height = usableHeightNow
                // Change the bounds of the root view to prevent gap between keyboard and content, and top of content positioned above top screen edge.
                rootView.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom)
                rootView.requestLayout()
    
                usableHeightPrevious = usableHeightNow
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 04:54

    Since the answer has already been picked and problem known to be a bug, I thought I would add a "Possible Work Around".

    You can toggle fullScreen mode when soft keyboard is shown. This allows the "adjustPan" to work correctly.

    In other words, I still use @android:style/Theme.Black.NoTitleBar.Fullscreen as part of the application theme and stateVisible|adjustResize as part of the activity window soft input mode but to get them to work together I must toggle fullscreen mode before the keyboard comes up.

    Use the following Code:

    Turn Off full screen mode

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    

    Turn On full screen mode

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    

    Note - inspiration came from: Hiding Title in a Fullscreen mode

    0 讨论(0)
提交回复
热议问题