LimpSquid
LimpSquid

Reputation: 327

Android alert dialog blank space when setting a view

I have a question. I made an alert dialog that is showing an message and view with a "do not show again" check-box. When i'm running this app on android KK it is displaying fine, however when I run it on android JB it is showing me a blank space like on this image(i also get this blank space when the view contains only a linear layout with a height of 0dp): http://foto.modelbouwforum.nl/images/2014/08/15/Screenshot2014-08-15-14-33-40.png

My code of the alert dialog:

final SharedPreferences sharedPreferences=getSharedPreferences("appPrefs", Context.MODE_PRIVATE);
        if(!sharedPreferences.getBoolean("isTutorialMainScreenChecked", false)){
            View checkBoxView = View.inflate(MainScreenHandler.this, R.layout.checkbox_alert_dialog,null);
            checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox);
            AlertDialog.Builder builder=new AlertDialog.Builder(this);
            builder.setMessage(getString(R.string.alertDialog_main_screen_tutorial));
            builder.setCancelable(false);
            builder.setView(checkBoxView);
            builder.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            if(checkBox.isChecked()){
                                SharedPreferences.Editor editor=sharedPreferences.edit();
                                editor.putBoolean("isTutorialMainScreenChecked", true);
                                editor.commit();
                            }
                            dialogInterface.dismiss();
                        }
                    });
            builder.setNegativeButton(getString(R.string.alertDialog_cancel),
                    new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialogInterface, int i){
                            dialogInterface.cancel();
                        }
                    });
            alertDialog = builder.create();
            alertDialog.show();

And here is my checkbox layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:orientation="vertical"
             android:baselineAligned="false">
    <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#ffff3bb2"/>

    <CheckBox
            style="@android:style/TextAppearance.Small"
            android:textColor="#ff000000"
            android:text="@string/alertDialog_checkbox_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/checkbox"/>
</LinearLayout>

Thanks in advance

P.s:

This is how it looks on android KK: http://foto.modelbouwforum.nl/images/2014/08/15/QuickMemo2014-08-15-14-42-41.png

Upvotes: 4

Views: 3939

Answers (3)

Yedige Omarbekov
Yedige Omarbekov

Reputation: 21

In addition to vokilam's answer:

If you're using android.support.v7.app.AlertDialog.

Tested on API 27 (pretty sure this works on API 28 too)

The layout:

<FrameLayout
    android:id="@+id/contentPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="48dp">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Space
                android:id="@+id/textSpacerNoTitle"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dialog_padding_top_material" />

            <TextView
                android:id="@+id/message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingEnd="?attr/dialogPreferredPadding"
                android:paddingStart="?attr/dialogPreferredPadding"
                style="@style/TextAppearance.Material.Subhead" />

            <Space
                android:id="@+id/textSpacerNoButtons"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dialog_padding_top_material" />
        </LinearLayout>
    </ScrollView>
</FrameLayout>

<FrameLayout
    android:id="@+id/customPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="48dp">

    <FrameLayout
        android:id="@+id/custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</FrameLayout>

The android:minHeight="48dp" for contentPanel and customPanel are causing the unwanted blank space.

Unwanted space dialog

What I did is:

alertDialog.SetView(view);
alertDialog.show();
...
View customPanel = (View) view.getParent().getParent();
if (customPanel != null)
    customPanel.setMinimumHeight(0);
View contentPanel = (View) alertDialog.findViewById(android.R.id.message).getParent().getParent().getParent();
if (contentPanel != null)
    contentPanel.setMinimumHeight(contentPanel.getMinimumHeight() * 2 / 3);

Result:

Blank space removed

Upvotes: 2

vokilam
vokilam

Reputation: 10313

I looked through sources of alert_dialog.xml layout in API 19 and found the following layout for inserting a custom view:

<FrameLayout android:id="@+id/customPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    <FrameLayout android:id="@+android:id/custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dip"
        android:paddingBottom="5dip" />
</FrameLayout>

Then I tried to set background to see which view takes the space. customPanel was colored in red, custom view was colored in green.

AlertDialog d = builder.create();
d.show();

View v = (View)d.getWindow().findViewById(android.R.id.custom).getParent();
v.setBackgroundColor(Color.RED);

enter image description here

This picture means that customPanel should have some paddings, or custom should have non-zero layout params or something else. After checking those attributes which were equal to zero I tried to test minimumHeight of the customPanel and got 192 on xxhdpi or 64dp. This setting cause the frame to be larger than a nested custom view.

I haven't figured out where this setting is applied, but here is the workaround:

AlertDialog d = builder.create();
d.show();

View v = (View)d.getWindow().findViewById(android.R.id.custom).getParent();
v.setMinimumHeight(0);

Tested on API 18,19.

Upvotes: 3

Ben Kauer
Ben Kauer

Reputation: 596

Did you try playing around with paddings and margins? You could copy your layout in res/layout-vXX and set a specific (negative) margin.

Upvotes: 0

Related Questions