0

I am showing an DialogFragment with a custom layout file. When the dialog is shown, the width is not matching the parent and everything is squished.

enter image description here

What am I doing incorrectly?

I call the MyDialog from a fragment.

 MyDialog().show(childFragmentManager, "myDialog")

MyDialog.kt

class MyDialog : DialogFragment() {

    private var _binding: DialogPlateStateBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        _binding = DialogPlateStateBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun onResume() {
        super.onResume()
        binding.root.requestLayout()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        binding.btnCancel.setOnClickListener { dismiss() }
    }
}

dialog_plate_state.xml

<androidx.constraintlayout.widget.ConstraintLayout 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="10dp">
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Update"
        android:textAlignment="center"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.example.libary.forms.FormPlateState
        android:id="@+id/form_plate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginVertical="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_title" />
    <Button
        android:id="@+id/btn_save"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="5dp"
        android:text="Save"
        android:textSize="10sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/btn_cancel"
        app:layout_constraintTop_toBottomOf="@id/form_plate"
        />

    <Button
        android:id="@+id/btn_cancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="5dp"
        android:text="Cancel"
        android:textSize="10sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@id/btn_save"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/form_plate"
         />

</androidx.constraintlayout.widget.ConstraintLayout>
user-44651
  • 3,924
  • 6
  • 41
  • 87

1 Answers1

1

Add this code in MyDialog class

override fun onStart() {
    super.onStart()
    dialog?.window?.setDimAmount(0.1f)
    dialog?.window?.setLayout(
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT
    )
}

override fun getTheme(): Int {
    return R.style.DialogTheme
}

Add style in styles.xml

<style name="DialogTheme" parent="Theme.AppCompat.Light.DialogWhenLarge">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">false</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:statusBarColor">@color/colorPrimaryDark</item>
      
    </style>
Sandesh Khutal
  • 1,712
  • 1
  • 4
  • 17