'Android Studio: Cannot get DialogFragment's inflated layout to match_parent
I have a Fragment on top of which I am showing a [popup] DialogFragment. The problem is that I cannot get the RelativeLayout which I'm inflating to fill the DialogFragment. Actually, it's always filling it horizontally; never vertically.
The [popup] DialogFragment (RelativeLayout) has layout_width and layout_height set to "match_parent".
The following screenshots show separate examples. In each case, the white rectangle is the (correctly sized) DialogFragment. The inflated XML is the red and yellow portion (with the TextView). This red and yellow is coming from a background drawable.
Notice that the inflated RelativeLayout always takes up the entire width and never the entire height. Note that I moved the embedded TextView (for the last two screenshots) to illustrate two points:
(1) It's the position of this TextView that's determining the height of the inflated layout.
(2) Its position has no effect on the width.
Also note:
The TextView's
layout_alignParentStartandlayout_alignParentTopare bothtrueIf I remove the TextView completely, the inflated layout "disappears" (presumably because I now have a "height" of zero).
Screenshots #2 and #5 (second column) are the same - except for the position of the TextView.
The same is true of #3 and #6 (third column).
(The TextView is mostly vertical in the red area in screenshot #6. It's very difficult to see here, but that's where it is - right of the yellow, with its bottom aligned to the bottom of the yellow area.)
What am I missing / doing wrong?
Code chunks:
ZzzDialog.kt (source of the [popup] DialogFragment):
package com.zazzem.two.dialogs
import android.app.Dialog
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
class ZzzDialog : DialogFragment() {
private lateinit var popup: View
private var popupLft: Int = 0
private var popupTop: Int = 0
private var popupWid: Int = 0
private var popupHgt: Int = 0
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val inflater = LayoutInflater.from(context)
popup = inflater.inflate(com.zazzem.two.R.layout.dialog_zzz, null)
return activity!!.let {
val builder = AlertDialog.Builder(it)
builder.setView(popup)
builder.create()
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val win = dialog!!.window!!
win.setGravity(Gravity.TOP or Gravity.LEFT) //top left first, so x & y below will be relative to there
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onResume() {
super.onResume()
val win = dialog!!.window ?: return
val params = win.attributes
params.x = popupLft
params.y = popupTop
params.width = popupWid
params.height = popupHgt
win.attributes = params
}
fun setPosAndSize(lft: Int, top: Int, wid: Int, hgt: Int) {
popupLft = lft
popupTop = top
popupWid = wid
popupHgt = hgt
}
}
the [TEMP] ClickListener (where the call to "setup" and show the [popup] is made from):
btnDevTemp.setOnClickListener {
fun doOne(l: Int, t: Int, w: Int, h: Int) {
dialog_specs.text = "Lft:$l Top:$t Wid:$w Hgt:$h"
val dlg = ZzzDialog()
dlg.setPosAndSize(l, t, w, h)
dlg.show(activity!!.supportFragmentManager, "ZzzDialog")
}
doOne(100,200,800,800) //pic #1
//doOne(10,10,1200,1900) //pic #2 and #5
//doOne(500,500,800,1200) //pic #3 and #6
//doOne(300,300,450, 900) //pic #4
}
XML for [popup] DialogFragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bkgr_zzz"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="151dp"
android:layout_marginTop="112dp"
android:backgroundTint="@color/Pink"
android:text="TextView"
/>
</RelativeLayout>
XML for the above's background:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<!-- yellow -->
<solid android:color="#FFFF00" />
<padding
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp"
/>
<!-- red -->
<stroke
android:width="25dp"
android:color="#FF0000"
/>
</shape>
Solution 1:[1]
Try this.
Get height of the dialog fragment programatically like this :
int height = getDialog().getWindow().getDecorView().getHeight();
Then, get a reference of that parent RelativeLayout and set it's height programatically as well.
relativeLayout.getLayoutParams().height = height;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Azhar92 |


