1

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?

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
casolorz
  • 8,486
  • 19
  • 93
  • 200

1 Answers1

1

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.

Community
  • 1
  • 1
Phiat
  • 506
  • 3
  • 12