casolorz
casolorz

Reputation: 9544

My custom alert dialog is much taller than it needs to be

Basically I have a RecyclerView in my AlertDialog (I've tried both the AppCompat one and the one from here) and when the list only has a few items the dialog is still as tall as it can be. Is there something I can do to fix this?

My layout is this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>

and I'm adding it to the AlertDialog like this:

LayoutInflater myLayout = LayoutInflater.from(context);
        final View dialogView = myLayout.inflate(R.layout.list, null);
dialog.setView(myLayout);

How can I make it wrap_content?

Upvotes: 1

Views: 129

Answers (1)

Phiat
Phiat

Reputation: 506

Here is another similar question I found. Using @PiyushMishra's solution:

alertDialog.show(); 
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 150;
lp.height = 500;
lp.x=-170;
lp.y=100;
alertDialog.getWindow().setAttributes(lp);

I was able to control the dialog's size. You could get creative with this and determine the size needed based on the # of list items.

I'm sorry I'm unsure how to get the Dialog to wrap_content. You may have some luck using a custom dialog instead of AlertDialog.

Upvotes: 1

Related Questions