Search code examples
androidkotlinandroid-viewbinding

ViewBinding with CardView Dialog Box not displaying width correctly


I'm having to migrate to ViewBinding in my Android app to comply with the latest target versions. I'm really struggling to get a dialog box created using CardView to display correctly.

The below is the script after I've made the modifications to use ViewBinding.

override fun addUserDialog() {
    Handler(Looper.getMainLooper()).post(Runnable {
        LoggerUtils.logMessage("PMN Calling MFA User Dialog")
        var dialog = Dialog(this)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setContentView(bindingMFA.root)
        dialog.setCancelable(false)
        bindingMFA.mBtnCancel.setOnClickListener {
            dialog.dismiss()
        }
        bindingMFA.mBtnConfirm.setOnClickListener {
            mMFACode = bindingMFA.mSMSMFA.text.toString()
            mLoginViewPresenter.confirmLogin(mMFACode)

            dialog.dismiss()
        }

        dialog.show()
        })

}

This appears within a file 'loginActivity.kt', where I set the binding within onCreate:

private lateinit var binding: ActivityLoginBinding
private lateinit var bindingMFA: DialogMfaBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityLoginBinding.inflate(layoutInflater)
    val view = binding.root
    setContentView(view)
    bindingMFA = DialogMfaBinding.inflate(layoutInflater)

If I change the line back to dialog.setContentView(R.layout.dialog_mfa) it shows the dialog box absolutely fine.

This is the dialog_mfa.xml file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="300dp"
android:layout_height="wrap_content" app:cardCornerRadius="4dp"
app:cardElevation="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" android:layout_margin="4dp">

    <TextView
        android:id="@+id/mLblInvitationHeader"
        style="@style/LabelTextMediumBold"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:background="@color/background_blue"
        android:gravity="center"
        android:text="@string/mfa_header"
        android:textColor="@color/text_white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/mLblHeader"
        style="@style/LabelTextMedium"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:gravity="center"
        android:singleLine="false"
        android:text="@string/mfa_intro"
        android:textColor="@color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mLblInvitationHeader" />


    <EditText
        android:id="@+id/mSMS_MFA"
        style="@style/LabelTextMedium"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:inputType="numberPassword"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:gravity="center"
        android:singleLine="true"
        android:textColor="@color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mLblHeader" />
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/mBtnCancel"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toStartOf="@+id/mBtnConfirm"
        android:layout_marginTop="16dp" app:layout_constraintTop_toBottomOf="@+id/mSMS_MFA"
        android:layout_marginStart="8dp" android:layout_marginEnd="4dp" android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent" style="@style/GeneralButtonGrey"
        android:text="@string/cancel"/>
    <Button
        android:text="@string/mfa_confirm"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/mBtnConfirm"
        app:layout_constraintStart_toEndOf="@+id/mBtnCancel" app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
        app:layout_constraintTop_toBottomOf="@+id/mSMS_MFA" android:layout_marginEnd="8dp"
        android:layout_marginStart="4dp" android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent" style="@style/GeneralButton"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

If anyone is able to point out what I'm doing wrong, it would be very much appreciated! Screenshot showing dialog box with incorrect width


Solution

  • Try the following:

    bindingMFA = DialogMfaBinding.inflate(layoutInflater)
    bindingMFA.window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);