开发者

How can we display progress icon in button

开发者 https://www.devze.com 2023-01-10 21:11 出处:网络
I need to display progress icon in button so that user can interact other GUI elements while background task is processing.

I need to display progress icon in button so that user can interact other GUI elements while background task is processing.

I have searched in Android developer site and fou开发者_Python百科nd that we can use animated drawables but don't know how to use them. Please advise on the same.


The very simple way to do this without using the animated drawable is to use "PregressBar" component in the design layout xml. When u need to show it, just set it's visibility property to visible and when you need to hide it, u can set it's visibility property to GONE. But remember this is UI task so when u need to do this with non-UI thread, u need to use Handler to set the status of "ProgressBar" component at runtime.

Below id the component in the layout file.

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

Below is the code written in java file

ProgressBar prg;
 @Override    
    public void onCreate(Bundle savedInstanceState) 
    {         
        super.onCreate(savedInstanceState);         
        setContentView(R.layout.main1); 
         prg=(ProgressBar)findViewById(R.id.ProgressBar1);
         prg.setVisibility(ProgressBar.GONE);
  }
public void start_background_process()
{
// starting the process
 prg.setVisibility(ProgressBar.VISIBLE);

new Thread(new Runnable() 
         { public void run() 
         { 
         // Do your background stuff here which takes indefinite time
        mHandlerUpdateProgress.post(mUpdateUpdateProgress);

         }
      } ).start();
}
final Handler mHandlerUpdateProgress= new Handler();

   final Runnable mUpdateUpdateProgress = new Runnable() {
       public void run() {
// ending the process
          prg.setVisibility(ProgressBar.GONE);
       }
   };


If the default progress indicator is good enough for you (i.e. the spinning wheel), then you can just use ProgressBar. To change it from a normal progress bar to a spinning wheel, use progressBar.setIndeterminate(true).

0

精彩评论

暂无评论...
验证码 换一张
取 消