31

I have a main activity that has simple buttons and listeners associated to them. Each button opens a new activity (intent). However while opening activity, it takes some time to load which causes an UI freeze. I want to avoid this by simply adding a loading circle (spinner) in between. I've searched many posts but still couldn't find anything.

By loading circle I mean this

image

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • I have a basic button listener with startActivity(intent) on click. And the resulting activity is a simple listview page which fetches data from a web service onCreate. My aim is to put a loading spinner in between these. –  Jan 29 '16 at 07:36
  • [Check this](http://stackoverflow.com/questions/21957263/why-the-background-of-progressdialog-doesnt-set-to-the-transparent/21957406#21957406) – M D Jan 29 '16 at 07:37

5 Answers5

37

That is a Progress Bar. You may create this programmatically or by using the widget in XML.

To implement in XML:

<ProgressBar
    android:id="@+id/progress_loader"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="visible" />

To implement in Java (using a Progress Dialog as you asked in comments, but you can also do this with the Progress Bar):

 ProgressDialog nDialog;
 nDialog = new ProgressDialog(Login.this);
 nDialog.setMessage("Loading..");
 nDialog.setTitle("Get Data");
 nDialog.setIndeterminate(false);
 nDialog.setCancelable(true);
 nDialog.show();

Before reaching a next activity, you should dismiss() the Progress Bar.

  nDialog.dismiss();
Parker
  • 8,539
  • 10
  • 69
  • 98
Parama Sudha
  • 2,583
  • 3
  • 29
  • 48
5

Just use below code in the second activity

<ProgressBar
    android:id="@+id/progress_loader"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="visible" />

make the visibility gone when you are done initializing

Suhaib Roomy
  • 2,501
  • 1
  • 16
  • 22
  • 2
    But what about if I have to show the progress bar going from Activity 1 to Activity 2. and dismiss it once second activity is loaded. – Rohan Feb 08 '18 at 00:16
  • @Rohan Make those activity fragments. And put in on the layout of the container activity (or even fragment). The purpose of an activity is to contain ALL the "activity". So it's weird that something is "active" (spinning) through two activities anyway. – Erdal G. Apr 15 '23 at 06:50
5

You cannot avoid the UI freezing by adding a loading widget. You will have to find the code that is blocking your main thread and move it to a worker thread. Look at "Helper classes for threading" in the Android documentation. Without showing your existing code (so we know what's blocking), we cannot advise you on this further.

Secondly, if you want to show a loading widget between view transactions, you could restructure your application to use fragments. An activity can be used to switch between your fragments, and it can also contain the widget which can be made visible before changing fragments and invisible once complete.

Charlie
  • 2,876
  • 19
  • 26
1

Try to do like this

ProgressDialog progressDoalog;

progressDoalog = new ProgressDialog(DetailsActivity.this);
        progressDoalog.setMax(100);
        progressDoalog.setMessage("Please wait...");
        progressDoalog.setTitle("My Application");
        progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDoalog.show();
        final Handler handle = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                progressDoalog.incrementProgressBy(1);
            }
        };
new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            while (progressDoalog.getProgress() <= progressDoalog
                                    .getMax()) {
                                Thread.sleep(30);
                                handle.sendMessage(handle.obtainMessage());
                                if (progressDoalog.getProgress() == progressDoalog
                                        .getMax()) {
                                    progressDoalog.dismiss();
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
Bhaskara Arani
  • 1,556
  • 1
  • 26
  • 44
0

2022 solution.

ProgressDialog p = new ProgressDialog(getActivity());
p.setMessage("Connecting ...");
p.setCancelable(false);
p.show();
  • you definitely want Cancelable false , otherwise users can just move it!

  • indeterminate does nothing, don't use

  • no title

  • once your server replies:

    p.dismiss();
    

enter image description here

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • 1
    I don't think so, as ProgressDialog is now deprecated? https://developer.android.com/reference/android/app/ProgressDialog – Victor Lee Aug 30 '22 at 13:32