728x90

googry.tistory.com/43

 

(Android) 키보드가 보여질 때 화면 스크롤하기

Activity에서 키보드가 올라오고 화면크기를 변경하기 위해 AndroidManifest.xml에 android:windowSoftInputMode에 adjustResize를 설정해줍니다. 위에 화면처럼 EditText를 선택하고 키보드가 올라오면 아래 로..

googry.tistory.com

키보트가 올라올때 바텀 네비게이션이 같이 올라오는 문제로

AndriudManifest 에서 android:windowSoftInputMode="adjustPan

설정 해봤지만 마음에 드는 동작이 안되서 키보드 올라오는 이벤트를 만들어 거기서 바텀 네비게이션을 하이드 시키기로 했다.

이유는 모르겠지만 안드로이드에는 키보드 올라오고 내려가는 리스너가 없다고 한다.

그래서 위에 링크처럼 만들어 봤다.

class KeyboardVisibilityUtils(
    private val window: Window,
    //private val onShowKeyboard: ((keyboardHeight: Int) -> Unit)? = null,
    private val onShowKeyboard: (() -> Unit)? = null,
    private val onHideKeyboard: (() -> Unit)? = null
) {

    private val MIN_KEYBOARD_HEIGHT_PX = 150

    private val windowVisibleDisplayFrame = Rect()
    private var lastVisibleDecorViewHeight: Int = 0


    private val onGlobalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener {
        window.decorView.getWindowVisibleDisplayFrame(windowVisibleDisplayFrame)
        val visibleDecorViewHeight = windowVisibleDisplayFrame.height()

        // Decide whether keyboard is visible from changing decor view height.
        if (lastVisibleDecorViewHeight != 0) {
            if (lastVisibleDecorViewHeight > visibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX) {
                // Calculate current keyboard height (this includes also navigation bar height when in fullscreen mode).
                //val currentKeyboardHeight = window.decorView.height - windowVisibleDisplayFrame.bottom
                // Notify listener about keyboard being shown.
                //onShowKeyboard?.invoke(currentKeyboardHeight)
                onShowKeyboard?.invoke()
            } else if (lastVisibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX < visibleDecorViewHeight) {
                // Notify listener about keyboard being hidden.
                onHideKeyboard?.invoke()
            }
        }
        // Save current decor view height for the next call.
        lastVisibleDecorViewHeight = visibleDecorViewHeight
    }

    init {
        window.decorView.viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener)
    }

    fun detachKeyboardListeners() {
        window.decorView.viewTreeObserver.removeOnGlobalLayoutListener(onGlobalLayoutListener)
    }
}

Class를 추가한다.

class MainActivity : AppCompatActivity() {
    private lateinit var keyboardVisibilityUtils: KeyboardVisibilityUtils

    //메모리에 올라갔을 때
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
  keyboardVisibilityUtils = KeyboardVisibilityUtils(window,
            onShowKeyboard = {
                homeConstrainLayout.run {
                    //smoothScrollTo(scrollX, scrollY + keyboardHeight)
                    //키보드 올라왔을때 원하는 동작
                    bottom_nav.visibility = View.GONE
                }
            },
            onHideKeyboard = {
                homeConstrainLayout.run {
                 	//키보드 내려갔을때 원하는 동작
                    //smoothScrollTo(scrollX, scrollY + keyboardHeight)
                    bottom_nav.visibility = View.VISIBLE
                }
            }
        )
    }
    
//메모리에 올라갔을 때 END

    override fun onDestroy() {
        keyboardVisibilityUtils.detachKeyboardListeners()
        super.onDestroy()
    }

Main에 추가한다.

 

동작은 되지만...

아쉽게도 순간적으로 바텀네비게이션이 보였다가 없어지는 잔상?! 같은것이 보였다.

다른 방법을 찾아 봐야겠다....

728x90

+ Recent posts