Context
A seamingly simple task, I want to set the height of an AlertDialog
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.
Goals
The question here refers to the soft goal, but I'd happily accept an answer for the hard goal!
Soft goal: Create an AlertDialog
having a custom empty ViewGroup
(or containing, say, an ImageView
), and set the dialog's width and height to a specific value.
Hard goal: Create an AlertDialog
displaying a square shape taking the most space possible, when the dialog's dimensions are given programatically.
My Tries, Condensed
java:
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.
alerttest.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Tried with and without the encapsulating FrameLayout. -->
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:layout_width="500dp"
android:layout_height="500dp">
<!-- Tried with a Space, an ImageView with a src. -->
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</FrameLayout>
Results: When it does not crash (for usually good reasons), only the width is set. Height is collapsed.
Related questions
- Android:How can I set the AlertDialog width and height,and the button of the AlertDialog style?
- Height of an AlertDialog
- How to implement a custom AlertDialog View
Thanks very much!