0

First, I will show you the BottomSheetDialogFragment class.

class BottomSheetReportFragment(var mContext: Context, var postId: String, var commentId: String?, var replyId: String?) : BottomSheetDialogFragment() {
    private lateinit var mView: View


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NORMAL, R.style.AppBottomSheetDialogTheme)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mView = inflater.inflate(R.layout.bottom_sheet_report, container, false)

        return mView
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)

        dialog.setOnShowListener { dialogInterface ->
            val bottomSheetDialog = dialogInterface as BottomSheetDialog
            setupRatio(bottomSheetDialog)
        }
        return dialog
    }

    private fun setupRatio(bottomSheetDialog: BottomSheetDialog) {
        val bottomSheet = bottomSheetDialog.findViewById<FrameLayout>(R.id.design_bottom_sheet)

        val behavior = BottomSheetBehavior.from(bottomSheet!!)
        val layoutParam = bottomSheet.layoutParams as ViewGroup.LayoutParams

        layoutParam.height = getBottomSheetDialogDefaultHeight()
        bottomSheet.layoutParams = layoutParam
        behavior.state = BottomSheetBehavior.STATE_COLLAPSED
        behavior.peekHeight = getBottomSheetDialogDefaultHeight()
    }

    private fun getBottomSheetDialogDefaultHeight(): Int {
        return getWindowHeight() * 85 / 100
    }

    private fun getWindowHeight(): Int {
        val displayMetrics = mContext.resources.displayMetrics
        return displayMetrics.heightPixels
    }
}

xml is like this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nested"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:overScrollMode="never">

        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:id="@+id/first"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/selector_button_report"
                android:button="@null"
                android:paddingStart="14dp"
                android:paddingEnd="14dp"
                android:text="1"
                android:textColor="#1C1C1C"
                android:textSize="16dp" />

            <Button
                android:id="@+id/second"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/selector_button_report"
                android:button="@null"
                android:paddingStart="14dp"
                android:paddingEnd="14dp"
                android:text="2"
                android:textColor="#1C1C1C"
                android:textSize="16dp" />

            <Button
                android:id="@+id/third"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/selector_button_report"
                android:button="@null"
                android:paddingStart="14dp"
                android:paddingEnd="14dp"
                android:text="3"
                android:textColor="#1C1C1C"
                android:textSize="16dp" />

            <Button
                android:id="@+id/fourth"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/selector_button_report"
                android:button="@null"
                android:paddingStart="14dp"
                android:paddingEnd="14dp"
                android:text="4"
                android:textColor="#1C1C1C"
                android:textSize="16dp" />

            <Button
                android:id="@+id/fifth"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/selector_button_report"
                android:button="@null"
                android:paddingStart="14dp"
                android:paddingEnd="14dp"
                android:text="5"
                android:textColor="#1C1C1C"
                android:textSize="16dp" />

            <Button
                android:id="@+id/sixth"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/selector_button_report"
                android:button="@null"
                android:paddingStart="14dp"
                android:paddingEnd="14dp"
                android:text="6"
                android:textColor="#1C1C1C"
                android:textSize="16dp" />

        </androidx.appcompat.widget.LinearLayoutCompat>

    </androidx.core.widget.NestedScrollView>


    <RelativeLayout
        android:id="@+id/relative"
        android:layout_width="match_parent"
        android:layout_height="91dp"
        android:visibility="gone"
        tools:visibility="visible">

        <EditText
            android:id="@+id/input"
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:background="@drawable/shape_edittext_comment"
            android:includeFontPadding="false"
            android:inputType="textMultiLine"
            android:lines="2"
            android:paddingStart="16dp"
            android:paddingEnd="73dp"
            android:textColor="#000000"
            android:textColorHint="#ADB1BA"
            android:textSize="14dp" />

        <ImageView
            android:id="@+id/send"
            android:layout_width="28dp"
            android:layout_height="28dp"
            android:layout_alignTop="@id/input"
            android:layout_alignBottom="@id/input"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="4dp"
            android:layout_marginEnd="20dp"
            android:layout_marginBottom="4dp"
            android:backgroundTint="#CBCBCB"
            android:src="@android:drawable/ic_menu_send" />

    </RelativeLayout>

</androidx.appcompat.widget.LinearLayoutCompat>

The Android keyboard is implemented so that any button from Button id first to sixth is pressed. But the problem here is that when the keyboard is up, the view focus is set on the EditText, but the EditText is obscured by the keyboard. Is there a way for BottomSheetDialogFragment to resize the view like Activity?

[UPDATE]

For reference, the style is applied as follows.

  1. adjustResize
  2. stateAlwaysVisible
  3. adjustResize, stateAlwaysVisible

I tried all three situations, but it didn't work.

<style name="AppBottomSheetDialogTheme" parent="Theme.Design.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/AppModalStyle</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowSoftInputMode">adjustResize|stateAlwaysVisible</item>
</style>

<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@drawable/shape_bottom_sheet</item>
</style>
hanjiman
  • 465
  • 1
  • 6
  • 17

1 Answers1

0

In your AppBottomSheetDialogTheme, add android:windowSoftInputMode.

<style name="AppBottomSheetDialogTheme">
    <item name="android:windowSoftInputMode">adjustResize</item>
</style>
fobidlim
  • 59
  • 5