How to resize an AlertDialog.Builder?

2019-01-29 09:50发布

问题:

I'd like to resize my AlertDialog.Builder but I didn't find any resolution on this website, I tried everything I saw on questions like this one but seems older than I'm looking for, The code I'm watching doesn't work right now...

ALERTDIALOGBUILDER.JAVA

public class AlertDialogInfo extends AlertDialog.Builder {

public AlertDialogInfo(Context context, String title, String msg) {
    super(context);
    AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.AppCompatAlertDialogStyle);
    builder.setTitle(title);
    builder.setMessage(msg);
    builder.setNegativeButton("OK",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    }
}

I tried to become AlertDialog.Builder to AlertDialog to resize it, but just displays a black square without data.

QUESTION: What should I do to make an AlertDialog.Builder with a large text? (need to scroll that sure, because I don't want to make the dialog as big as my screen)

UPDATED:

What I want is something like this, I HAVE TO CHANGE THE TEXT by Bundle, I call the AlertDialog from my AppCompatActivity which uses FrameLayout (I don't know what kind of Dialog it should be to cover what I need):

UPDATED 2:

Finally I've been trying to do what I'm looking for with the next code, but it still doesn't work...

ALERT_DIALOG.XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="250dp">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollbars="vertical">

    <TextView
        android:id="@+id/msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp" />

</LinearLayout>

MYALERTDIALOG.JAVA

public class MyDialogFragment extends DialogFragment {

public static MyDialogFragment newInstance(String text) {
    MyDialogFragment frag = new MyDialogFragment();
    Bundle args = new Bundle();
    args.putString("TEXT", text);
    System.out.print("Text dentro de newInstance: " + text);
    frag.setArguments(args);
    return frag;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    String sinopsis = getArguments().getString("TEXT");
    System.out.print("Text dentro de onCreateDialog: " + text);
    return new AlertDialog.Builder(getActivity())
            .setIcon(android.R.drawable.ic_dialog_info)
            .setTitle("TITLE")
            .setMessage(text)
            .setNegativeButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dismiss();
                        }
                    }
            ) .create();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.alert_dialog, container);
}
}

ActivityWhereDialogIsBeenCalled.java

........
DialogFragment frag = MyDialogFragment.newInstance("MY MESSAGE");
        frag.show(getSupportFragmentManager(),"TAG");
...............

My dialog appears using DEFAULT LAYOUT (It doesn't have scrollview nor my interested size (just shows as default android alert dialog...)

How can I resolve this question? I think that's silly, but it's frustating... Thanks anyway!

回答1:

You should use custom dialog layout, check documentation.

Actually, you can resize dialog by getDialog().getWindow().setLayout(width, height); just call it onResume (not onCreateView).

Your view must be nested inside of a ScrollView, check post.

Reading huge amount of text in a Dialog can be annoying, it's better to use all pixels you have, consider simple Activity/Fragment.



回答2:

If you're using an AlertDialog within a DialogFragment, with the AppCompat support library 23.2.1 or above, you must put the resize code in DialogFragment.onStart(), otherwise it will not work. Something like this:

@Override
public void onStart() {
    super.onStart();
    // Method 1
    alertDialog.getWindow().getAttributes().width = 400; // pixels
    alertDialog.getWindow().getAttributes().height = 500; // pixels
    // Re-apply the modified attributes
    alertDialog.getWindow().setAttributes(
           alertDialog.getWindow().getAttributes());

    // Method 2 (alternative)
    alertDialog.getWindow().setLayout(400, 500); // size in pixels
}

More info:

  • http://babe.ilandroid.com/alertdialog-with-custom-view-resize-to-wrap-the-views-content.html
  • https://maven.google.com (list of available support library versions for gradle)