1

I'm trying to build a simple custom alert dialog. Just a header, and edit text, and a button. Problem is that the dialog is too small to properly display the widgets. I've tried to implement quite a few suggestions, but none of them solve the issue. How can I expand this to a larger size? What am I doing wrong? Thanks!

My code is:

LayoutInflater inflater = LayoutInflater.from(this);
View builderView = inflater.inflate(R.layout.enter_password, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(builderView);   
AlertDialog alert = builder.create();   
alert.show();

The layout xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:background="@drawable/background_gray_gradient" >

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="70dp"
            android:layout_gravity="left"

            android:contentDescription="@string/blank"
            android:src="@raw/lock" >
        </ImageView>    

        <TextView
            android:layout_marginTop="15dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:paddingTop="5dip"
            android:paddingLeft="5dp"
            android:text="@string/password_to_continue_title"
            android:textSize="20sp" />

    </LinearLayout>

    <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        android:hint="@string/password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
            android:id="@+id/submit_password"
            style="@style/ButtonText"
            android:onClick="submitPassword"
            android:layout_gravity="center_horizontal"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/gray_button"
            android:text="@string/continue_on" />

</LinearLayout>
Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51
AndroidDev
  • 20,466
  • 42
  • 148
  • 239

1 Answers1

3

This answer just satisfies your requirements, https://stackoverflow.com/a/10912076/826657

       Rect displayRectangle = new Rect();
        Window window = this.getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
        LayoutInflater inflater = LayoutInflater.from(this);
        View builderView = inflater.inflate(R.layout.activity_dia, null);
        builderView.setMinimumWidth((int) (displayRectangle.width() * 0.9f));
        builderView.setMinimumHeight((int) (displayRectangle.height() * 0.9f));
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(builderView);
        AlertDialog alert = builder.create();
        alert.show();
Community
  • 1
  • 1
Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51
  • As far as I can see this just locks the dialogue as "huge" rather than dynamically resizing to be "just large enough" to fit the contents – Richard Tingle Sep 17 '14 at 20:15
  • @RichardTingle that was what the question required. – Rachit Mishra Sep 17 '14 at 20:20
  • Obviously this is wildly after the fact and am only commenting because I'm experiencing the same problem. But my reading of "Just a header, and edit text, and a button. Problem is that the dialog is too small to properly display the widgets" is that the dialogue needs to be (for example) 40% of the screen but is actually (enfuriatingly) 30% of the screen – Richard Tingle Sep 17 '14 at 20:23
  • @RichardTingle can't we adjust the size by decreasing multiplication factor ? – Rachit Mishra Sep 17 '14 at 20:39
  • @twntee Of course, at which point it isn't dynamic and it becomes a nightmare to support different screen sizes (where the font sizes might be different, or even worse user changable) – Richard Tingle Sep 17 '14 at 20:40
  • @RichardTingle i think this code should be on basis of window width & pixel density of device, then i hope it will work fine. i will give a shot and reply back. – Rachit Mishra Sep 17 '14 at 20:43
  • 2
    Hmm, I seem to have got the dynamic "wrap content" behaviour I wanted. Seemingly it was getting upset because some views were "fill_parent" on their width (which actully made the width behave exactly as I wanted - the child being as wide as the widest of the other children) but upset the height. By making sure all children were either "wrap_content" or explicitly set it seems happy. This actually looks like the OPs problem as well. But I can't really understand why it gets upset (or at least why it gets upset in this way, if the width went crazy that would make sense but the heigh goes mad) – Richard Tingle Sep 17 '14 at 20:48