0

I currently have the following code to build a wait dialog with a ProgressBar:

LayoutInflater factory = LayoutInflater.from(TherapistActivity.this);
View view = factory.inflate(R.layout.waitdialog, null);
dialog = new AlertDialog.Builder(TherapistActivity.this)
    .setView(view)
    .setCancelable(false)
    .create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();

wmlp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
wmlp.x = 0;   //x position
wmlp.y = Math.round(metrics.density * 100);   //y position

wmlp.width = Math.round(metrics.density * 55);  //doesn't appear to work

Here is the XML for my dialog:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/top"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:background="@drawable/boxbkg">

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"/>


</RelativeLayout>

I would like my dialog to be a small square dialog with just a spinning ProgressBar. However, even with wmlp.width = Math.round(metrics.density * 55), the dialog remains wide.

What is the proper way to get around this?

Pink Jazz
  • 784
  • 4
  • 13
  • 34
  • `dialog` is an `AlertDialog` and you have to call `dialog.show()` at the end, otherwise it won't display anything. Unfortunately this doesn't work, even with the additional code from the accepted answer. – Neph Apr 28 '20 at 09:42

3 Answers3

0

As far as I know, changing dialog - params / width and height - should take place in the onCreate (set style is needed) and in the onCreateDialog (setting the params).

example of this two methods from a custom DialogFragment which take place on the whole screen without any margin:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    Window w = dialog.getWindow();
    w.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
    WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
    params.verticalMargin = 0;
    params.horizontalMargin = 0;
    params.x=0;
    params.y = 0;
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    params.width= ViewGroup.LayoutParams.MATCH_PARENT;
    params.height = ViewGroup.LayoutParams.MATCH_PARENT;
    w.setGravity(Gravity.TOP| Gravity.LEFT);

    return dialog;
}

Please let me know if it helped you.

Roee
  • 1,155
  • 3
  • 15
  • 24
0

I figured it out. It appears that I needed two more lines of code. Before setting the width, I needed this line of code:

wmlp.copyFrom(dialog.getWindow().getAttributes());

And after setting the width, I needed this line of code:

dialog.getWindow().setAttributes(wmlp);
Pink Jazz
  • 784
  • 4
  • 13
  • 34
  • I tried this but it didn't work: The dialog never changed width, no matter what I set it to (using a `RelativeLayout` with `wrap_content`). No idea if this is caused by using a `Fragment`. – Neph Apr 28 '20 at 09:40
0

The original code, in combination with the extra code from the answer, doesn't work with my code unfortunately, which might be caused by using a Fragment.

The following code (tested with Java 8/min API 24/target API 27) works with a Fragment and with both portrait and landscape screen orientation. It lets you set the size of the dialog to whatever you like (see below) and there's no need to set its position because in the end it's still just a normal AlertDialog (without buttons or title) with a custom view:

AlertDialog.Builder builder = new AlertDialog.Builder((AppCompatActivity) getActivity());
builder.setView(R.layout.progress_view);
AlertDialog loadingDialog = builder.create();
loadingDialog.show();
loadingDialog.getWindow().setLayout(400,400); //You have to call this after "show"!
//loadingDialog.dismiss();

My progress_view layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/progress_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="#FF0000"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="20dp"
    android:paddingEnd="20dp"
    android:paddingStart="20dp"
    android:paddingTop="20dp">

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

I tried using a LinearLayout before but it didn't work, the dialog still used its original size. The important bit here is match_parent because only that actually centers the progress circle in the AlertDialog. Using wrap_content is going to push it to the top left of the dialog.

The result looks like this:

enter image description here

You can of course add more stuff, like e.g. a "Please wait..." TextView or set the background color to whatever color you want, just two things I noticed:

  1. Transparency doesn't work. If you set the background to e.g. 50% alpha, you can see the white background of the AlertDialog through it. I tried changing the dialog's transparency as suggested here (custom style or getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT))) but, even though it worked, the dialog and ended up losing its rounded corners and got squished.
  2. You have to test what size works for you. 400 by 400 is fine for a default progress circle and some padding but if you make the dialog too small for the view's content, it won't show up at all.
Neph
  • 1,823
  • 2
  • 31
  • 69