How to achieve a good-looking progress bar in Android development
This article mainly shows you "how to achieve a good-looking progress bar in Android development", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve a good-looking progress bar in Android development" this article.
Activity_main.xml MainActivity Java file code
In the MainActivity.java file, our first step is to initialize all the progress bar views, and then write three functions of type void, writing code for each progress bar.
We used a handler in each function. In Android, we can't run long-term tasks on the main thread; that's why we use handlers. Handlers allow communication with UI threads from other background threads.
MainActivity.javapackage com.progressbar.example.mainactivity;import android.os.Bundle;import android.os.Handler;import androidx.appcompat.app.AppCompatActivity;import androidx.appcompat.widget.Toolbar;import android.view.Menu;import android.view.MenuItem;import android.widget.ProgressBar;import android.widget.Toast;import com.progressbar.example.R;import com.progressbar.example.utils.Tools;public class MainActivity extends AppCompatActivity {private ProgressBar progressDeterminate; private ProgressBar progressIndeterminateCircular; private ProgressBar progressBuffered; private ProgressBar progressIndeterminateDeterminate @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); initComponent ();} private void initComponent () {progressDeterminate = (ProgressBar) findViewById (R.id.progressDeterminate); progressIndeterminateCircular = (ProgressBar) findViewById (R.id.progressIndeterminateCircular); progressBuffered = (ProgressBar) findViewById (R.id.progressBuffered) ProgressIndeterminateDeterminate = (ProgressBar) findViewById (R.id.progressIndeterminateDeterminate); startDeterminateProgress (); startBufferedProgress (); startBufferedSecondaryProgress (); startIndeterminateDeterminateProgress (); startDeterminateCircularProgress ();} private void startDeterminateProgress () {final Handler mHandler = new Handler (); Runnable runnable = new Runnable () {public void run () {int progress = progressDeterminate.getProgress () + 10 ProgressDeterminate.setProgress (progress); if (progress > 100) {progressDeterminate.setProgress (0);} mHandler.postDelayed (this, 1000). These are all the contents of the article "how to achieve a good-looking progress bar in Android development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!