How to resize an AlertDialog.Builder?
Asked Answered
R

2

0

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

WhatIWant

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!

Rieger answered 21/4, 2016 at 18:23 Comment(2)
mkyong.com/android/android-custom-dialog-exampleSelfjustifying
I’m voting to close this question because .Rieger
Q
0

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.

Quijano answered 21/4, 2016 at 18:31 Comment(4)
Thanks you Maxim, could you tell me how to make not fullscreen a DialogFragment? It seems the same error...Rieger
I already saw this post, and seems to be what I'm looking for, thanks. But what's the best way to change the message of the dialog using Bundle since the Activity where Dialog is calling??Rieger
Same as for any Fragment, repo linkQuijano
I tried dislikedialog... but cannot shrink my dialog fragment... It is still filling as large as text beRieger
H
-1

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:

Hateful answered 28/12, 2017 at 23:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.