1

I try to create an alert dialog with the customed layout. I set the width with ViewGroup.LayoutParams.MATCH_PARENT, but the alert dialog is wider than the screen. Could you please give me some suggestions? Here are my codes and the XML file.

    val builder = androidx.appcompat.app.AlertDialog.Builder(context)
    builder.setView(layoutResId)
    val dialogue=builder.create()
    dialogue.window?.setBackgroundDrawableResource(android.R.color.transparent)

    val lp= dialogue.window?.attributes
    if (lp != null) {
        lp.width=ViewGroup.LayoutParams.MATCH_PARENT
        lp.gravity= Gravity.BOTTOM
    }
    dialogue.window?.attributes=lp

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:paddingTop="5dp"
    android:background="@drawable/four_rounded_corners_white"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView
    android:layout_width="match_parent"
    android:layout_height="43dp"
    android:text="Selected Time"
    android:textColor="@color/j5_blue"
    android:gravity="center"
    android:textSize="17sp"
    android:textStyle="bold"/>
</LinearLayout>
Talha Saglam
  • 17
  • 1
  • 5
Anna ZOU
  • 11
  • 4

0 Answers0