ProgressBar, an Android development entry component

Keywords: Android

Today, I'll talk about the progress bar (actually including the refresh circle). I'll mainly talk about some basic and common methods. First, I'll look at the pictures to know what I'm going to talk about.

1. Basic usage

The default progress bar is a rotating refresh circle, i.e. without parameters

 <ProgressBar
        android:id="@+id/pb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        style="@android:style/Widget.ProgressBar"/>

    <ProgressBar
        android:id="@+id/pb3"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_marginTop="10dp"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        />

The above code shows two rotating arcs, with the effect as follows:

But how to write a progress bar

<ProgressBar
        android:id="@+id/pb3"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_marginTop="10dp"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        />//Set the style of progress bar

    <ProgressBar
        android:id="@+id/pb4"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_marginTop="10dp"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:max="100"   //Maximum length of progress bar
        android:progress="10"  //Selection length of progress bar
        android:secondaryProgress="30"//Second Selection Len gt h of Progress Bar/>

You need to set the style to change the progress bar style for operation. The effect is as follows:

2. Control function of progress bar

For example, set a button to operate the progress bar to display the progress bar.

bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handler.sendEmptyMessage(0);
            }
        });

Handler handler=new Handler(){
        public void handleMessage(Message msg){
            super.handleMessage((msg));
            if (pb3.getProgress()<100){
               handler.postDelayed(runnable,500);
            }else {
                ToastUtil.showMessage(ProgressActivity.this,"Load complete");
            }
        }
    };

    Runnable runnable=new Runnable() {
        @Override
        public void run() {
            pb3.setProgress(pb3.getProgress()+5);
            handler.sendEmptyMessage(0);
        }
    };

 

 

Posted by KMC1499 on Tue, 15 Oct 2019 10:10:41 -0700