Android

[Android] Progressbar 사용하기

Hoplin 2022. 4. 7. 23:43
반응형

Progress Bar은 진행 정도를 표시하거나, 작업이 진행중임을 사용자에게 알려야한다. 안드로이드에는 크게 두가지 모형의 Progress Bar이 존재한다

  • 막대모양 : 작업의 진행 정도를 알려줄 수 있도록 막대 모양으로 표시
  • 원 모양 : 작업이 진행중임을 알려준다. 원모양으로된 프로그레스바가 반복적으로 표시된다.

Progress Bar의 XML 태그는 <ProgressBar>이며,프로그래스 바가 갖는 최대 범위는 max속성으로, 현재 값은 progress속성으로 나타내준다. 현재값인 progress속성은 메소드인 setProgress(int progress)를 통해서 설정해 줄 수 있다.

<ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:progress="40"
        android:max="100" />

예제 소스코드를 보자.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:textSize="30sp"
        android:hint="Text will be typed here"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="BT1"/>
        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="BT2" />

    </LinearLayout>
</LinearLayout>
package com.example.sampleprogress;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
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 {
    ProgressDialog dialog;
    ProgressDialog dialog2;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView1);
        ProgressBar progressBar = findViewById(R.id.progressBar);
        progressBar.setIndeterminate(false);
        //progressBar.setIndeterminate(true);
        progressBar.setProgress(80);
        Button btn = findViewById(R.id.btn1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog = new ProgressDialog(MainActivity.this);
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                //dialog.setIndeterminate(true);
                dialog.setCancelable(true);
                //dialog.setCancelable(false); ProcessDialog를 취소할 수 있는지를 결정한다.
                // 디폴트는 true인듯하다
                dialog.setMessage("Checking Data");
                dialog.show();
            }
        });
        Button btn2 = findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog2 = new ProgressDialog(MainActivity.this);
                dialog2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                dialog2.setCancelable(true);
                dialog2.setMessage("Test Horizontal style progress bar");
                dialog2.show();
            }
        });
    }
}

몇가지 주요 메소드를 살펴보자.

 

< ProgressBar >

  • setIndeterminate(boolean)
    • Indeterminate(작업의 종료가 불확실) / Determinate(작업의 종료가 확실)  속성 중 하나를 선택하는 메소드이다. true인 경우 Indeterminate, false인 경우 Determinate로 설정된다. 디폴트로는 Indeterminate로 설정된다.
    • ProgressBar에는 두가지 속성이 있다
      • Indeterminate는 불확정적 상태표시모드이다. 명확한 수치 또는 범위 값을 사용하지 않고, 막연히 작업이 진행되고 있음을 표시할때 사용하는 속성이다.
      • Determinate는 확정적 상태표시모드이다. 진행상태를 표시할 때 명확한 수치 혹은 범위값을 지정하여 현재의 진행단계를 표시할 때 사용하는 속성이다.
  • setProgress : ProgressBar의 현재 작업 진행 백분율값을 지정해 준다. 당연히 이 값은 Determinate속성일때만 제대로 사용할 수 있다.

 < ProgressBarDialog : API 26 버전(Android Oreo)부터는 Deprecated되었음 >

  • ProgressDialog : ProgressBar을 Dialog형태로 나타나게 끔 하는 객체이다. 
  • setProgressStyle(int(상수)) : 일반적인 ProgressBar
    • ProgressDialog.STYLE_SPINNER : 원형 progress bar dialog가 생성된다.
    • ProgressDialog.STYLE_HORIZONTAL : 막대 progressbar dialog가 생성된다 
  • setCancelable(Boolean) : ProgressDialog를 중간에 취소할 수 있는지의 여부를 결정한다
    • true : 중간에 취소할 수 있다
    • false : 중간에 취소할 수 없다(ProgressDialog가 dismiss()를 통해서 취소될 때 까지)
  • setMessage(String) : ProgressDialog에 표시할 메세지를 넘겨준다
  • show() : ProgressBarDialog를 사용자에게 보여준다
  • dismiss() : ProgressBar Dialog를 종료한다

반응형