본문 바로가기
App Programming/Android Studio

[Android Studio] 프로그레스바 (ProgressBar)

by goatlab 2022. 10. 31.
728x90
반응형
SMALL

프로그레스바 (ProgressBar)

 

ProgressBar는 작업의 진행 상태를 사용자에게 알려줄 때 사용하는 컴포넌트이다.

 

xml

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ProgressBar
        android:id="@+id/progressBarCircle"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="76dp"
        android:layout_height="76dp"
        android:layout_gravity="center"
        android:indeterminate="false"
        android:max="100"
        android:progressBackgroundTint="#D8D2D2"
        android:progressDrawable="@drawable/circle_progressbar"
        android:progressTint="#33D12A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

xml : circle_progressbar

 

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape
            android:innerRadiusRatio="2.71"
            android:thicknessRatio="7.6"
            android:shape="ring"
            android:useLevel="false"
            android:type="sweep">
            <solid android:color="#9E9E9E" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <rotate
            android:fromDegrees="270"
            android:toDegrees="270">
            <shape
                android:innerRadiusRatio="2.71"
                android:thicknessRatio="7.6"
                android:shape="ring"
                android:angle="0"
                android:type="sweep">
            </shape>
        </rotate>
    </item>

</layer-list>

 

java

 

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    private ProgressBar progressBar;
    private int progressStatus = 0;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar) findViewById(R.id.progressBarCircle);
        
        // Start long running operation in a background thread
        new Thread(new Runnable() {
            public void run() {
                while (progressStatus < 100) {
                    progressStatus += 1;
                    // Update the progress bar and display the
                    //current value in the text view
                    handler.post(new Runnable() {
                        public void run() {
                            progressBar.setProgress(progressStatus);
                        }
                    });
                    try {
                        // Sleep for 200 milliseconds.
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

728x90
반응형
LIST