Search code examples
androidandroid-alertdialogandroid-dialog

Android alert dialog custom panel and button panel height is 0


The function i am using to create the dialog.Alert dialog custtomPanel and buttonPanel both have 0 height when created with a large array. How can i fix the problem thanks.

 private AlertDialog createDialog() {
    String[] alphabet = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    //String[] alphabet = new String[]{"a", "b", "c", "d", "e", "f", "g", "h"};
    final boolean[] selectedAlphabets = new boolean[alphabet.length];
    int tempPosition = 0;
    for (String tempItem : alphabet) {
        selectedAlphabets[tempPosition] = false;
        tempPosition++;
    }

    View view = getLayoutInflater().inflate(R.layout.dialog_message_view, null);
    final TextView warning_message_tv = (TextView) view.findViewById(R.id.dialog_message);
    warning_message_tv.setText("-");


    final AlertDialog.Builder builder = new AlertDialog.Builder(this)
            .setTitle("Alert Dialog")
            .setCancelable(false)
            .setView(view)
            .setPositiveButton("Save",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    }
            ).setMultiChoiceItems(alphabet, selectedAlphabets, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    selectedAlphabets[which] = isChecked;

                    int trueCount = 0;
                    for (int i = 0; i < selectedAlphabets.length; i++) {
                        if (selectedAlphabets[i]) {
                            trueCount++;
                        }
                    }
                    if (trueCount > 0) {
                        warning_message_tv.setText("* "+ trueCount + " items is selected");
                    }else {
                        warning_message_tv.setText("-");
                    }
                }
            }).setCancelable(true);

    return builder.create();
}

so when i use the shorter array there is no problem the buttons and custom view is visible but when i use the longer array my positive button and custom view height are both 0

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
   >

        <TextView
            android:id="@+id/dialog_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="5dp"
            android:textColor="#808080"
            android:minLines="2"
            android:gravity="center"
            android:text="* Lorem ipsum dolor sit amet, consectetur adipiscing elit."
            android:textSize="12sp" />


</LinearLayout>

dialog with short array {"a", "b", "c", "d", "e", "f", "g", "h"} dialog with short array layout inspector height is 196 dialog array with all alphabet values dialog with all alphabet values layout inspector height is 0


Solution

  • Try this method :

        String[] alphabet = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                LinearLayout rootLayout = new LinearLayout(this);
                rootLayout.setOrientation(LinearLayout.VERTICAL);
                ScrollView scrollView = new ScrollView(MainActivity.this);
                LinearLayout layout = new LinearLayout(this);
                layout.setOrientation(LinearLayout.VERTICAL);
                layout.setPadding(50, 0, 50, 0);
        
                for (int i = 0; i < alphabet.length; i++) {
                    CheckBox checkBox = new CheckBox(this);
                    checkBox.setText(alphabet[i]);
                    checkBox.setTextColor(Color.parseColor("#ff0000"));
                    checkBox.setTextSize(24);
                    checkBox.setPadding(10,5,10,5);
                final int finalI = i;
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        Log.i("Onclick", finalI +" ->"+isChecked);
                    }
                });
                    layout.addView(checkBox);
                }
                scrollView.addView(layout);
                rootLayout.addView(scrollView);
                alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
        
                    }
                });
                alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        
                    }
                });
                alertDialog.setView(rootLayout);
                alertDialog.show();