Android - how to hide / show progressbar onclick

前端 未结 3 775
粉色の甜心
粉色の甜心 2021-01-28 22:52

I am trying to show a progressbar when a button is clicked. When i test the app it force closes / stops. My app works fine before the progressbar code is added in.

also

相关标签:
3条回答
  • 2021-01-28 23:14

    try following code...

    handler1 = new Handler()
    {
                @Override
                public void handleMessage(Message msg) 
                {
                    switch(msg.what)
                    {
                    case 1:
                        static_class.digi_pd = ProgressDialog.show(Create_Digitizing_Job.this, "Loading...", "Please Wait..", true,false);
                        static_class.digi_pd.setCancelable(false);
                        break;
                    case 2:
                        static_class.digi_pd.dismiss();
                        break;
    
                    }
                }
            };
    
            thread1 = new Thread()
              {
                    @Override
                    public void run()
                    {   
                           try
                            {  
    
    
                              handler1.sendEmptyMessage(1);
    
                        // write your code here....
    
                             handler1.sendEmptyMessage(3);
                             handler1.sendEmptyMessage(2);
    
                            } 
                           catch (Exception e) 
                           {
                               Log.w("thread error...",""+e);
                               //e.printStackTrace();
                           }   
    
                 }
    
             };
            thread1.start();
    
    0 讨论(0)
  • 2021-01-28 23:16

    try this way, i am not sure it will help you or not

    b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final ProgressDialog progress = ProgressDialog.show(THENAMEOFYOURACTIVITYCLASS.this,
                    ProgressTitle, ProgressMessage, true, false);
    
            new Thread(new Runnable() {
                public void run() {
                loadFeed();
                progress.cancel();
                }
            }).start();
        }
    });
    
    Be careful about what loadFeed(statutory); do. Because now is working inside a THREAD and Threads con not modify UI, If this is the case then you should do something like this:
    
    b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final ProgressDialog progress = ProgressDialog.show(THENAMEOFYOURACTIVITYCLASS.this,
                    ProgressTitle, ProgressMessage, true, false);
    
            new Thread(new Runnable() {
                public void run() {
                loadFeed(); //just load data and prepare the model
                runOnUiThread(new Runnable() {
                                 @Override public void run() {
                                   //here you can modify UI here
                                   //modifiying UI element happen here and at the end you cancel the progress dialog
                                   progress.cancel();
                             }
                        }); // runOnUIthread
    
                }
            }).start();
        }
    });
    
    0 讨论(0)
  • 2021-01-28 23:16
                ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(R.style.NewDialog);
        progressDialog.setMessage("Loading...");
    

    Paste the below in your styles.xml New Dialog :

    <style name="NewDialog">
    <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>
    

    When you want to show you can cuse progressDialog.show() and to hide you can use progressDialog.dismiss().

    0 讨论(0)
提交回复
热议问题