Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

概要

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
        }
    }
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away