0

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.

The asymmetrical AlertDialog I get

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?

Robb1
  • 4,587
  • 6
  • 31
  • 60

2 Answers2

1

I'm not sure if it is a solution for you, but you can use GridLayout from support library. Then, having layout like that:

<android.support.v7.widget.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="match_parent"
    android:layout_height="wrap_content"
    app:columnCount="3"
    app:rowCount="3">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

</android.support.v7.widget.GridLayout>

You will have result like that:

enter image description here

Divers
  • 9,531
  • 7
  • 45
  • 88
  • I get a lot of errors for most of attributes you used, for example: `Error:(1) No resource identifier found for attribute 'columnCount' in package 'my.package.path.here'`. What's wrong? Thanks for the answer btw! @Divers – Robb1 Jan 03 '17 at 13:50
  • Are you sure you've read my answer? `you can use GridLayout from support library. ` and so on. Are you using GridLayout from support library? – Divers Jan 03 '17 at 13:57
  • I'm sorry but as I'm a newbie I don't know how to import that library... could you be more specific? Thanks for the patience! – Robb1 Jan 03 '17 at 13:58
  • 1
    @Robb1 Please check answers here - http://stackoverflow.com/questions/17292812/how-to-add-android-support-v7-widget-gridlayout-into-intellijidea – Divers Jan 03 '17 at 14:07
0

You could use a 'DialogFragment' instead of an 'AlertDialog'. It gives you more freedom to customize it. You can find a tutorial about it here: https://android-developers.googleblog.com/2012/05/using-dialogfragments.html

Katharina
  • 1,612
  • 15
  • 27