Tutorials  /  Security

Creating a Custom Progress Bar in Android

Ccentron Redaktion · October 2024 ·4 min read ·Security, Tutorial

Progress bars are essential UI elements in Android applications, particularly for showing the status of ongoing tasks. Customizing these progress indicators allows you to maintain a unique look and feel for your app. This guide will walk you through creating a custom progress bar using Android's built-in features.

Basic Circular Progress Bars

Let's first look at a simple way to display a progress indicator. The following example shows three circular ProgressBar elements aligned vertically within a ConstraintLayout.

Code
< ?xml version="1.0" encoding="utf-8"?>

In this layout, you have three types of progress bars (Large, Small, and Medium), each with its own size and position within the view. The progress bars will rotate endlessly, signaling an ongoing background task.

SEC

Matching infrastructure at centron

Hardening does not stop at the server: cloud firewalls filter traffic before it reaches the VM – centrally managed, no per-rule surcharge. Explore cloud firewalls →

Adding a Custom ProgressBar with an Icon

Next, let’s take it up a notch by adding an icon to the ProgressBar. The indeterminateDrawable attribute allows you to replace the default rotating indicator with a custom image or icon.

Here’s the updated layout where an icon spins in place of the standard progress bar indicator:

Code
< ?xml version="1.0" encoding="utf-8"?>

At first glance, the icon may appear static. This is because we need to animate it. To do so, we’ll utilize the RotateDrawable to define the animation parameters, such as rotation angle and pivot points.

Using RotateDrawable for a Spinning Effect

A RotateDrawable is created by wrapping a drawable (like an image or icon) inside a rotate tag. You can set parameters like the rotation angle and speed. Here's how you can define a rotating drawable in progress_icon.xml:

Code
< ?xml version="1.0" encoding="utf-8"?>

In this example, the toDegrees attribute defines the number of degrees for one full rotation. You can modify this value to adjust the speed of the rotation. A full rotation is generally represented by 360 degrees.

Now, update the layout to use this drawable in your ProgressBar:

Code
&lt ProgressBar
    android:id="@+id/progressBar"
   
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminateDrawable="@drawable/progress_icon"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" >

A Basic Example with Progress Delays

Let’s combine the rotating progress bar with a delayed action. The following code displays a random message from a list after a few seconds of loading, mimicking a common use case of showing a loading spinner before content appears.

XML Layout (activity_main.xml)

Code
<?xml version="1.0" encoding="utf-8"?>


    

    

MainActivity.java Code

Go
package com.example.customprogressbar;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    TextView textView;
    List quotesList;
    ProgressBar progressBar;
    int i = 0;

    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        quotesList = new ArrayList<>();

        quotesList.add("Hi");
        quotesList.add("Happy New Year");
        quotesList.add("Hope you have a good day");
        quotesList.add("Merry Christmas");

        Button btnTap = findViewById(R.id.button);
        textView = findViewById(R.id.textView);
        progressBar = findViewById(R.id.progressBar);

        btnTap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setVisibility(View.VISIBLE);
                textView.setVisibility(View.GONE);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        if (i == quotesList.size())
                            i = 0;

                        textView.setVisibility(View.VISIBLE);
                        textView.setText(quotesList.get(i++));
                        progressBar.setVisibility(View.GONE);

                    }
                }, 3000);
            }
        });
    }
}

Final Thoughts

This guide demonstrates how to create a custom rotating progress indicator in Android by using RotateDrawable. This feature provides a smooth and customizable way to display loading animations that fit your app’s branding. You can implement this in various scenarios to enhance the user experience while waiting for background tasks to complete.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Security
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren