Empty android alert dialog when customizing view

I have a question. I made an alert dialog that displays a message and a view with a "do not show again" checkbox. When I run this app in android KK it displays fine, however when I run it on Android hardware it shows me blank space like in this image (I also get this blank space when the view only contains a linear layout with height 0dp): http://foto.modelbouwforum.nl/images/2014/08/15/Screenshot2014-08-15-14-33-40.png

My alert dialog code:

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>

      

Thank you in advance

Ps:

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

+3


source to share


3 answers


I looked alert_dialog.xml

at layout sources in API 19 and found the following layout to insert 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 the background to see which view was taking up space. customPanel

was colored red, the custom view was colored 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 image means it customPanel

must have some shims, or custom

must have non-zero layout options or whatever. After checking for those attributes that were zero, I tried to test minimumHeight

customPanel

and got 192 at xxhdpi or 64dp. This option causes the frame size to be larger than the nested custom view.

I didn't understand where this setting applies, but here's a workaround:

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

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

      

Tested against API 18.19.

+3


source


Have you tried playing with paddings and margins? You can copy the layout to res / layout-vXX and set a specific (negative) margin.



0


source


In addition to the vocals, I will answer:

If you are using android.support.v7.app.AlertDialog.

Tested on API 27 (sure it works on API 28 too)

Location:

<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>

      

android:minHeight="48dp"

for contentPanel and customPanel cause unwanted white space.

Unwanted space dialogue

What I did was:

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:

Empty space removed

0


source







All Articles