Note that there are some explanatory texts on larger screens.

plurals
  1. POSet the height of an AlertDialog with a custom view of zero intrisic size
    primarykey
    data
    text
    <h2>Context</h2> <p>A seamingly simple task, I want to set the height of an <code>AlertDialog</code> containing a custom view. Going through the dozen of SO questions on this subject, I came up with 12! possible answers. After having tried all of them (that's 15 years at a try per second), it seems that banging my head on the screen has the same effect: none. The reason I want to do that is that my custom view has no intrisic dimensions, it just spans the space it is given.</p> <hr> <h2>Goals</h2> <p>The question here refers to the soft goal, but I'd happily accept an answer for the hard goal!</p> <p><strong>Soft goal:</strong> Create an <code>AlertDialog</code> having a custom empty <code>ViewGroup</code> (or containing, say, an <code>ImageView</code>), and set the dialog's width and height to a specific value.</p> <p><strong>Hard goal:</strong> Create an <code>AlertDialog</code> displaying a <em>square</em> shape taking the most space possible, when the dialog's dimensions are given programatically.</p> <hr> <h2>My Tries, Condensed</h2> <p><strong>java:</strong></p> <pre><code>View layout = getLayoutInflater ().inflate (R.layout.alerttest, null); AlertDialog alert = new AlertDialog.Builder (this) .setPositiveButton(R.string.alert_dialog_ok, null) .setTitle ("Don't cry, will you?") // .setView (layout) may be done here. .create(); alert.show (); // alert.setView (layout) here, or before .show, or I can use the doc's: FrameLayout fl = (FrameLayout) alert.findViewById (android.R.id.custom); fl.addView (layout, new LayoutParams (500, 500)); // Arbitrary dimensions alert.getWindow ().setLayout (800, 600); // Or use before show (), or use alert.getWindow ().setAttributes with // a WindowManager.LayoutParams crafted from getWindow ().getAttributes. // Tried before and after show. </code></pre> <p><strong>alerttest.xml:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;!-- Tried with and without the encapsulating FrameLayout. --&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt; &lt;LinearLayout android:layout_width="500dp" android:layout_height="500dp"&gt; &lt;!-- Tried with a Space, an ImageView with a src. --&gt; &lt;ImageView android:layout_width="match_parent" android:layout_height="match_parent"/&gt; &lt;/LinearLayout&gt; &lt;/FrameLayout&gt; </code></pre> <p><strong>Results:</strong> When it does not crash (for usually good reasons), only the width is set. Height is collapsed.</p> <hr> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/11856550/androidhow-can-i-set-the-alertdialog-width-and-height-and-the-button-of-the-ale">Android:How can I set the AlertDialog width and height,and the button of the AlertDialog style?</a></li> <li><a href="https://stackoverflow.com/questions/5648477/height-of-a-alertdialog">Height of an AlertDialog</a></li> <li><a href="https://stackoverflow.com/questions/2795300/how-to-implement-a-custom-alertdialog-view">How to implement a custom AlertDialog View</a></li> </ul> <p>Thanks very much!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload