androidkotlinandroid-textinputlayoutmaterial-dialog

How to prevent automatic resizing of MaterialAlertDialogBuilder height when the keyboard is open?


I have built a custom dialog with MaterialAlertDialogBuilder. The dialog has 2 TextInputLayouts. The problem is when I try to write something in the TextInputEditText and when the keyboard is visible, the MaterialAlertDialogBuilder shrinks in height.

In XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="vertical"
    android:padding="16dp"
    tools:viewBindingIgnore="true">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="দোয়া"
        app:endIconMode="clear_text"
        app:shapeAppearance="@style/ShapeAppearance.App.SmallComponent"
        tools:viewBindingIgnore="true">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/doarName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="11dp"
        android:hint="দৈনিক কতবার পাঠ করবেন?"
        app:endIconMode="clear_text"
        app:shapeAppearance="@style/ShapeAppearance.App.SmallComponent"
        tools:viewBindingIgnore="true">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number" />

    </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>


In Kotlin:

MaterialAlertDialogBuilder(this@TosbiActivity).setView(view).setTitle("দোয়া যুক্ত করুন")
                .setPositiveButton("Apply") { dialog, _ ->

                    /*
                    ......
                     */

                    dialog.dismiss()
                }.setNegativeButton("Cancel") { dialog, _ ->
                    dialog.dismiss()
                }.show()

The output of ths code:

(when the keyboard is hidden)

(when the keyboard is hidden).

(when the keyboard is visible)

(When the keyboard is visible)

What am I tried?

I checked by adding the android:windowSoftInputMode="adjustResize"/> and android:windowSoftInputMode="adjustPan" codes in activity tag of the manifest, but it doesn't work. And also I searched or asked about this on the internet + ChatGPT AI but got no correct answer.


Solution

  • In the mentioned problem the dialog is created using MaterialAlertDialogBuilder. But the problem is that when the dialog is shown on a small screen, clicking on the input field is resizing the dialog box. Below is a simple way to solve this problem.

    The problem can be solved in two ways.

    Alert Dialog Builder Code:

     val dialog = MaterialAlertDialogBuilder(this)
                .setView(View.inflate(this,R.layout.dialog, null))
                .setTitle("দোয়া যুক্ত করুন")
                .setPositiveButton("Apply") { d, _ ->
                    d.dismiss()
                }.setNegativeButton("Cancel") { d, _ ->
                    d.dismiss()
                }.create()
    

    The first one:

    dialog.window?.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN)
    

    The second:

    dialog.window?.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
    

    Adding either WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS to the constant will hopefully work.

    Note: window.addFlage must be set before showing the dialog

    The code snippet:

      val dialog = MaterialAlertDialogBuilder(this)
                        .setView(View.inflate(this,R.layout.dialog, null))
                        .setTitle("দোয়া যুক্ত করুন")
                        .setPositiveButton("Apply") { d, _ ->
                            d.dismiss()
                        }.setNegativeButton("Cancel") { d, _ ->
                            d.dismiss()
                        }.create()
    
            
       dialog.window?.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN)
       dialog.show()