title: Determine keyboard presence and change layout accordingly in Jetpack Compose
body: <p>When I click on TextField, I need to scroll UI upwards to show login button to the user and not hide it behind keyboard.</p>
  <p>I am using RelocationRequester for the same.</p>
  <p>I am using this for detecting keyboard show/hide event:</p>
  <pre><code>fun listenKeyboard() {
  val activityRootView =
    (requireActivity().findViewById&lt;View&gt;(android.R.id.content) as ViewGroup).getChildAt(0)
  activityRootView.viewTreeObserver.addOnGlobalLayoutListener(object :
    ViewTreeObserver.OnGlobalLayoutListener {
    private var wasOpened = false
    private val DefaultKeyboardDP = 100
    private val EstimatedKeyboardDP =
      DefaultKeyboardDP + 48
    private val r: Rect = Rect()
    override fun onGlobalLayout() {
      val estimatedKeyboardHeight = TypedValue
        .applyDimension(
          TypedValue.COMPLEX_UNIT_DIP,
          EstimatedKeyboardDP.toFloat(),
          activityRootView.resources.displayMetrics
        )
        .toInt()
  
      activityRootView.getWindowVisibleDisplayFrame(r)
      val heightDiff: Int = activityRootView.rootView.height - (r.bottom - r.top)
      val isShown = heightDiff &gt;= estimatedKeyboardHeight
      if (isShown == wasOpened) {
        return
      }
      wasOpened = isShown
      keyboardVisibleState(isShown)
    }
  })
    }
  </code></pre>
  <p>and once the keyboard is visible, I am calling the relocationRequestor's bringIntoView().</p>
  <pre><code>coroutineScope.launch {     
      delay(250)
      relocationRequester.bringIntoView()
  }
  </code></pre>
  <p>Its behaving randomly, working on some devices and not on others. Is there any better solution to deal with this issue?</p>