Display progressdialog without text Android
Asked Answered
G

13

33

Android 2.3.3

I have a progressdialog that shows, Loading.. as text. Here is the code for the progressdialog .

progressDialog = new ProgressDialog(mContext);      
            progressDialog.setIndeterminate(true);
            progressDialog.setMessage("Loading...");
            progressDialog.show();

If I remove the line progressDialog.setMessage("Loading...");, I get a progressdialog of the left and an empty box on the right that occupies the width of the parent.

I want to display only the progressdialog , aligned at the center. Please refer to the images below..

This is what i have...

enter image description here

This is what i want...

enter image description here

Can someone help me with this?

Gabrila answered 7/6, 2013 at 9:12 Comment(0)
S
74

Try this 1.create a method like this :

public static ProgressDialog createProgressDialog(Context context) {
    ProgressDialog dialog = new ProgressDialog(context);
    try {
        dialog.show();
    } catch (BadTokenException e) {

    }
    dialog.setCancelable(false);
    dialog.getWindow()
        .setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog.setContentView(R.layout.progressdialog);
    // dialog.setMessage(Message);
    return dialog;
}

// Xml Layout :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@android:color/transparent" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

and call this method wherever you want :

if (progressDialog == null) {
    progressDialog = Utils.createProgressDialog(Login.this);
    progressDialog.show();
} else {
    progressDialog.show();
}
Selfsupport answered 7/6, 2013 at 9:32 Comment(5)
I get the following crash/error: "requestFeature() must be called before adding content"Honeycutt
i tried all the solutions offered. this one is the best. it shows a nice clean progress spinner without a box and without text.Sendoff
At least in Lollipop, the spinner does show a box for me, with the typical width and background colour of a dialog.Amorita
The solution for Lollipop is to run the following before setContentView: dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));Amorita
@all ->> see my answer below <<--Ledaledah
A
23

If you happen to get the error : "requestFeature() must be called before adding content", the solution is to call progressDialog.show() BEFORE you call progressDialog.setContentView(R.layout.progressdialog).

Aime answered 5/8, 2014 at 16:54 Comment(1)
superb @CholetskiCohla
L
8

Working and looking good on material design with accent color (if you have them)

Tried hard to make it work on both 5.0+ and lower API version. And the solution is to make it with a dialog instead of a progress.

in layout folder create a file aux_progress_spinner.xml

<?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="match_parent"
              android:background="@android:color/transparent"
              android:gravity="center"
              android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        />

</LinearLayout>

Create a function somewhere...let's say Utils.class

   public static Dialog LoadingSpinner(Context mContext){
        Dialog pd = new Dialog(mContext, android.R.style.Theme_Black);
        View view = LayoutInflater.from(mContext).inflate(R.layout.aux_progress_spinner, null);
        pd.requestWindowFeature(Window.FEATURE_NO_TITLE);
        pd.getWindow().setBackgroundDrawableResource(R.color.transparent);
        pd.setContentView(view);
        return pd;
    }

In your fragment/activity call:

 private Dialog progress_spinner;
 progress_spinner = Utils.LoadingSpinner(mContext);   

 //------ Where you want it ------
 progress_spinner.show();

 //------- Dismiss it --------
 progress_spinner.dismiss();

Optional: add a simple CountDownTimer to test how it looks

        new CountDownTimer(1000,1000){

            @Override public void onTick(long millisUntilFinished) {

            }

            @Override public void onFinish() {
                progress.dismiss();
            }
        }.start();
Ledaledah answered 28/7, 2015 at 10:30 Comment(0)
S
7

you can add below style in your style.xml

<style name="progress_bar_style">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:background">@android:color/transparent</item>
</style>

where you add any color in color.xml file.

Sungsungari answered 7/6, 2013 at 9:29 Comment(5)
@VamsiChalla you want style to write programmatically java code ? or you want how to use this style in java code ?Sungsungari
I want to do whatever the above xml code does, programmatically with java code. I want to style the progressdialog programmatically.Gabrila
first way you use Dialog dialog = new Dialog(mContext,"your stylename"); or other you can check style programmatilly set by code in https://mcmap.net/q/108858/-set-style-for-textview-programmaticallySungsungari
@BenH what is your requirment ? we can set by programmatically or by styleSungsungari
my requirement is the same as the OP's. a spinner without a box around it, and centered.Sendoff
D
5

You can use:

Progress progress = new ProgressDialog(getActivity(), android.support.v4.app.DialogFragment.STYLE_NO_TITLE);

If necessary, add to Gradle dependencies

compile 'com.android.support:support-v4:22.2.0'
Disfavor answered 12/7, 2015 at 22:38 Comment(1)
it shows a black square in progress dialog background with your method, although I have setBackgroundDrawable as transparent, any idea?Lactose
P
1

Use ProgressBar instead of ProgressDialog. http://developer.android.com/reference/android/widget/ProgressBar.html

Poorly answered 7/6, 2013 at 9:14 Comment(2)
Use the style="?android:attr/progressBarStyle" in xmlPoorly
Try setting the layout parameters programatically. I'm not sure about this though.Poorly
D
1

you can use :

progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

and if you want no title you can :

progressDialog.setTitle(null);
Dissimulation answered 20/9, 2013 at 10:6 Comment(0)
Q
1

MuraliGanesan's answer is correct, but I use just Dialog, not ProgressDialog to avoid "requestFeature() must be called before adding content" exception

Quent answered 6/7, 2015 at 5:17 Comment(0)
M
1

You can do this by just creating one class which extends Dialog class:

Let's say I have created below class:

public class TransparentProgressDialog extends Dialog {

    public ImageView iv;

    public TransparentProgressDialog(Context context, int resourceIdOfImage) {
        super(context, R.style.TransparentProgressDialog);
        WindowManager.LayoutParams wlmp = getWindow().getAttributes();
        wlmp.gravity = Gravity.CENTER_HORIZONTAL;
        getWindow().setAttributes(wlmp);
        setTitle(null);
        setCancelable(false);
        setOnCancelListener(null);
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        iv = new ImageView(context);
        iv.setImageResource(resourceIdOfImage);
        layout.addView(iv, params);
        addContentView(layout, params);
    }

    @Override
    public void show() {
        super.show();
        RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatCount(Animation.INFINITE);
        anim.setDuration(3000);
        iv.setAnimation(anim);
        iv.startAnimation(anim);
    }
}

Put TransparentProgressDialog in style.xml:

@null @android:color/transparent true @null @null @android:style/Animation.Dialog stateUnspecified|adjustPan true @android:color/transparent

Now you can put any progress image which will circulate:

TransparentProgressDialog pDialog = new TransparentProgressDialog(mContext, R.drawable.progress);
pDialog.show();

That's it.

Mozarab answered 2/3, 2016 at 5:57 Comment(1)
can you show me the style for the progress dialog? please :DWaggon
J
1

Based on MuraliGanesan answer I created a subclass of progressDialog.

public class LoadingDialog extends ProgressDialog {

    public LoadingDialog(Context context) {
        super(context);
    }

    @Override
    public void show() {
        super.show();
        setCancelable(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.custom_progress_dialog);
    }
}

Also you have to create the custom_progress_dialog.xml in res/layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:layout_gravity="center">

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

And you use it as easy as this:

LoadingDialog dialog = new LoadingDialog(this);
dialog.show();
:
:
dialog.dismiss();
Jolty answered 25/7, 2017 at 20:38 Comment(0)
A
0

This work for me in MonoDroid:

progressDialog.SetMessage((string)null);

Try this in java:

progressDialog.setMessage(null);
Astro answered 20/3, 2014 at 21:30 Comment(1)
This does not solve the issue; the icon still appears on the left and the rest of the dialog is blank.Amorita
A
0
ProgressDialog.show(this, null,"", true);
Anglesey answered 19/7, 2017 at 21:32 Comment(0)
C
0

What I have done is..

private ProgressDialog progressDialog;

private void loadProgressDialog() {
    try {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("Please Wait...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
    } catch (Exception e) {
        e.printStackTrace();
    }
}


private void showProgressDialog() {
    if (progressDialog != null && !progressDialog.isShowing()) {
        progressDialog.show();
    }
}


private void hideProgressDialog() {
    if (progressDialog != null && progressDialog.isShowing()) {
        progressDialog.dismiss();
    }
}

Output

Col answered 24/5, 2020 at 16:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.