Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android loading circle (spinner) between two activity

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();

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


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.


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();