SoFunction
Updated on 2025-03-11

Android implements dynamic visible and hidden progress bar

This article shares the specific code for Android to implement dynamic visible and hidden progress bars for your reference. The specific content is as follows

Call

(this, new () {
                @Override
                public void progress(int count) {
                    (count + "%");
                }
            }); 

ProgressUtil

package com.coral3.common_module.utils;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import com.coral3.common_module.R;
import ;
import ;
import ;

public class ProgressUtil {

    private static View progressContainer;
    private static TextView tvView;
    private static ProgressBar progressView;
    private static ViewGroup contentView;
    private static Timer timer = new Timer();
    private static TimerTask task;
    private static int count = 0;
    private static ICallback myICallback;
    private static Handler handler = new Handler(new (){

        @Override
        public boolean handleMessage(Message msg) {
            if( == 0x1){
                count++;
                (count);
                (count + "%");
                (count);
            }
            return false;
        }
    });

    public static void startProgress(Context context, ICallback iCallback){
        if(null == contentView) contentView = ((Activity)context).findViewById();
        if (progressContainer == null) {
            progressContainer = (context).inflate(.view_progress, null, false);
            progressView = (.pb_common);
            tvView = (.tv_progress);
            (progressContainer);
        } else {
            ();
        }
        myICallback = iCallback;
        task = new TimerTask() {
            @Override
            public void run() {

                if(count > 99){
                    hideProgressInUiThread((Activity) context);
                }else{
                    (0x1);
                }
            }
        };
        if(timer == null) timer = new Timer();
        (task, 10, 1000/60);
    }

    public static void endTimer(){
        ();
        ();
        task = null;
        timer = null;
        count = 0;
    }

    public static void hideProgress(){
        if (progressContainer != null) {
            endTimer();
            ();
        }
    }

    public static void startProgressInUiThread(Context context, ICallback iCallback){
        ((Activity)context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                startProgress(context, iCallback);
            }
        });
    }

    public static void hideProgressInUiThread(Activity activity){
        (new Runnable() {
            @Override
            public void run() {
                hideProgress();
            }
        });
    }

    public interface ICallback{
        void progress(int count);
    }
}

view_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:andro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:gravity="center"
            android:padding="8dp"
            android:layout_height="match_parent">
            <ProgressBar android:
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:progress="10"
                style="@style/"></ProgressBar>
            <TextView
                android:
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0%"/>
        </LinearLayout>

</RelativeLayout>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.