LLO 7.1 – Implement Progress Bar in Android Application

LLO 7.1 – Implement Progress Bar in Android Application

Course Outcome – Develop Android applications using UI components and layouts


1. Concept (Brief)

A Progress Bar visually indicates the progress of a long-running task such as downloading a file, uploading assignments, or loading data.

Types:

  • Indeterminate – unknown duration (spinner animation)

  • Determinate – known duration (e.g., 0–100%)


2. Android Project Code

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.progressbarapp">

    <application
        android:allowBackup="true"
        android:label="Study Material Downloader"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="20dp">

    <!-- Title -->
    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sanjay Ghodawat Polytechnic"
        android:textSize="20sp"
        android:textStyle="bold"
        android:paddingBottom="10dp" />

    <TextView
        android:id="@+id/txtSub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Downloading Study Material"
        android:textSize="16sp"
        android:paddingBottom="20dp" />

    <!-- Indeterminate Progress -->
    <ProgressBar
        android:id="@+id/progressIndeterminate"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <!-- Determinate Progress -->
    <ProgressBar
        android:id="@+id/progressDeterminate"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="0"
        android:paddingTop="20dp" />

    <!-- Percentage Display -->
    <TextView
        android:id="@+id/txtPercent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Progress: 0%"
        android:paddingTop="10dp" />

    <!-- Start Button -->
    <Button
        android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Download"
        android:paddingTop="20dp" />

</LinearLayout>

MainActivity.java (Java Version)

package com.example.progressbarapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ProgressBar progressDeterminate, progressIndeterminate;
    TextView txtPercent;
    Button btnStart;

    Handler handler = new Handler(Looper.getMainLooper());
    int progressStatus = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressDeterminate = findViewById(R.id.progressDeterminate);
        progressIndeterminate = findViewById(R.id.progressIndeterminate);
        txtPercent = findViewById(R.id.txtPercent);
        btnStart = findViewById(R.id.btnStart);

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progressStatus = 0;
                progressDeterminate.setProgress(0);
                txtPercent.setText("Progress: 0%");
                progressIndeterminate.setVisibility(View.VISIBLE);
                btnStart.setEnabled(false);

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (progressStatus < 100) {
                            progressStatus++;

                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    progressDeterminate.setProgress(progressStatus);
                                    txtPercent.setText("Progress: " + progressStatus + "%");
                                }
                            });

                            try {
                                Thread.sleep(80);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                progressIndeterminate.setVisibility(View.GONE);
                                btnStart.setEnabled(true);
                                Toast.makeText(
                                        MainActivity.this,
                                        "Study Material Download Completed",
                                        Toast.LENGTH_LONG
                                ).show();
                            }
                        });
                    }
                }).start();
            }
        });
    }
}

res/values/strings.xml

<resources>
    <string name="app_name">Study Material Downloader</string>
</resources>

3. How it Works (Step-by-Step)

  1. Student clicks Start Download

  2. Indeterminate Progress Bar shows immediate activity

  3. Determinate Progress Bar increases from 0 to 100%

  4. When download completes, a message appears:

    “Study Material Download Completed”


4. How Students Can Run This

  1. Open Android Studio

  2. Create Empty Activity Project

  3. Replace the files with the above code

  4. Run on:

    • AVD Emulator

    • Or physical Android device

It will work on default API level 21+.


5. Learning Benefits for Students

Students will understand:

  • UI components

  • Threads and Handlers

  • Real-world application behavior

  • Feedback in user interface design


6. Directory Structure 

ProgressBarApp/
│
├── app/
│   ├── src/
│   │   ├── main/
│   │   │   ├── AndroidManifest.xml
│   │   │   ├── java/
│   │   │   │   └── com/
│   │   │   │       └── example/
│   │   │   │           └── progressbarapp/
│   │   │   │               └── MainActivity.java
│   │   │   └── res/
│   │   │       ├── layout/
│   │   │       │   └── activity_main.xml
│   │   │       ├── values/
│   │   │       │   └── strings.xml
│   │   │       └── mipmap-anydpi/
│   │   │           └── ic_launcher.xml
│   │   └── test/
│   └── build.gradle
└── settings.gradle


Popular posts from this blog