1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Androidのソフトウェアキーボードの高さと表示状態をお手軽取得

Last updated at Posted at 2020-02-21

概要

Androidのキーボードの高さと表示されているかを知る方法が標準で用意されていないので自作してみました.
そのままコピペすれば動きます.
コードは10行くらいです.

ソースコード

    var layoutBottom: Int = 0 // 現在のlayoutのbottomを保存
    var isShowingKeyboard: Boolean = false // キーボードが表示されていればtrue
    var keyboardHeight: Int = 0 // キーボードの高さ
    override fun onCreate(savedInstanceState: Bundle?) {
        val mRootView = window.decorView.findViewById<ViewGroup>(android.R.id.content)
        layoutBottom = mRootView.height
        mRootView.viewTreeObserver.addOnGlobalLayoutListener {
            val rect = Rect()
            mRootView.getWindowVisibleDisplayFrame(rect)
            isShowingKeyboard = layoutBottom > rect.bottom
            keyboardHeight = abs((layoutBottom - rect.bottom))
            layoutBottom = rect.bottom
        }
    }
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?