I have an xml whose root is a GridLayout and I want it to be displayed inside an AlertDialog.
Here's the code I used inside the onResume() of my fragment:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View addViewImg = inflater.inflate(R.layout.matrix_image, null);
builder.setView(addViewImg);
AlertDialog alertDialog = builder.create();
Window window = alertDialog.getWindow();
//I tried with "setLayout" but it doesn't work
window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
alertDialog.show();
Here's: matrix_image.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/matrixLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imageUpLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/sym_def_app_icon" />
<!-- Other 8 ImageButtons composing a 3x3 grid --/>
</GridLayout>
The problem is that the View is not centered inside the AlertDialog... nor the AlertDialog wraps around the GridLayout.
How to get rid of the white space on the right and wrap the AlertDialog around the GridLayout?
Note: I tried with several options proposed here on stackoverflow, but none of them works for me. Maybe the GridLayout behaves somehow differently?

