@@ -312,6 +312,13 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
312312 clearClipboardContent()
313313 toggleClipboardVisibility(false )
314314 }
315+
316+ suggestions_holder.addOnLayoutChangeListener(object : OnLayoutChangeListener {
317+ override fun onLayoutChange (v : View ? , left : Int , top : Int , right : Int , bottom : Int , oldLeft : Int , oldTop : Int , oldRight : Int , oldBottom : Int ) {
318+ updateSuggestionsToolbarLayout()
319+ suggestions_holder.removeOnLayoutChangeListener(this )
320+ }
321+ })
315322 }
316323
317324 val clipboardManager = (context.getSystemService(Context .CLIPBOARD_SERVICE ) as ClipboardManager )
@@ -1632,18 +1639,32 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
16321639 } else {
16331640 mToolbarHolder?.autofill_suggestions_holder?.addView(it)
16341641 }
1635-
1636- // make room on suggestion toolbar for inline views
1637- mToolbarHolder?.suggestions_items_holder?.gravity = Gravity .NO_GRAVITY
1638- mToolbarHolder?.clipboard_value?.maxWidth = resources.getDimensionPixelSize(R .dimen.suggestion_max_width)
1642+ updateSuggestionsToolbarLayout()
16391643 }
16401644 }
16411645
16421646 @RequiresApi(Build .VERSION_CODES .R )
16431647 fun clearClipboardViews () {
16441648 mToolbarHolder?.autofill_suggestions_holder?.removeAllViews()
1645- // restore original clipboard toolbar appearance
1646- mToolbarHolder?.suggestions_items_holder?.gravity = Gravity .CENTER_HORIZONTAL
1647- mToolbarHolder?.clipboard_value?.maxWidth = Integer .MAX_VALUE
1649+ updateSuggestionsToolbarLayout()
1650+ }
1651+
1652+ private fun updateSuggestionsToolbarLayout () {
1653+ if (hasInlineViews()) {
1654+ // make room on suggestion toolbar for inline views
1655+ mToolbarHolder?.suggestions_items_holder?.gravity = Gravity .NO_GRAVITY
1656+ mToolbarHolder?.clipboard_value?.maxWidth = resources.getDimensionPixelSize(R .dimen.suggestion_max_width)
1657+ } else {
1658+ // restore original clipboard toolbar appearance
1659+ mToolbarHolder?.suggestions_items_holder?.gravity = Gravity .CENTER_HORIZONTAL
1660+ mToolbarHolder?.suggestions_holder?.measuredWidth?.also { maxWidth ->
1661+ mToolbarHolder?.clipboard_value?.maxWidth = maxWidth
1662+ }
1663+ }
16481664 }
1665+
1666+ /* *
1667+ * Returns true if there are [InlineContentView]s in [autofill_suggestions_holder]
1668+ */
1669+ private fun hasInlineViews () = (mToolbarHolder?.autofill_suggestions_holder?.childCount ? : 0 ) > 0
16491670}
0 commit comments