android loading circle (spinner) between two activity

自古美人都是妖i 提交于 2020-06-24 11:09:09

问题


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


回答1:


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



回答2:


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.




回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/35079083/android-loading-circle-spinner-between-two-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!