메뉴 건너뛰기

조회 수 447 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

1. ProgressBar 구현예제

먼저 앱의 메인 화면을 구성하는 XML 레이아웃 리소스입니다. 

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/cureentValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CureentValue"
            android:textAlignment="center" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ProgressBar
                android:id="@+id/h_progressbar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:layerType="none" />

            <EditText
                android:id="@+id/editText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:width="50dp"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="" />

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="1"
                android:gravity="right"
                android:onClick="ClickHandler"
                android:text="실행"
                android:textAlignment="center" />
        </LinearLayout>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

 

메인 화면 모습

 

▼ ProgressBar / EditText / TextView / Button 위젯이 배치된 형태입니다. EditText로부터 입력된 정수형 데이터를 Max 값으로 하여 0~Max값까지 반복문을 통하여 Count 하는 예제입니다. AsyncTask를 통해 구현하며 반복문을 돌면서 현재 Value 값(작업 진행 정도 수치 값)을 통하여 작업 진행 정도를 ProgressBar를 통해 표시하도록 합니다. 먼저 Main 화면을 실행하는 자바 소스코드입니다. 

public class MainActivity extends AppCompatActivity {

    ProgressBar myProgressBar;
    EditText myEditText;
    TextView myTextView;

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

        this.initializeView();
    }

    public void initializeView()
    {
        myProgressBar = (ProgressBar)findViewById(R.id.h_progressbar);
        myEditText = (EditText)findViewById(R.id.editText);
        myTextView = (TextView)findViewById(R.id.cureentValue);
    }

    public void ClickHandler(View view)
    {
        int value = Integer.parseInt(myEditText.getText().toString());
        MyAsyncTask asyncTask = new MyAsyncTask(value, myTextView, myProgressBar);
        asyncTask.execute();
    }
}

▼ InitializeView() 함수는 레이아웃 리소스에 정의된 View들의 참조를 얻어오는 함수입니다. 

 

 

 

 

▼ ClickHandler() 함수는 버튼 클릭에 대한 이벤트 처리를 담당하는 함수입니다. 해당 함수에서는 EditText의 Text 값을 읽어와 int형 변수에 저장합니다. 그런 다음 구현된 AsynTask를 생성하면서 필요한 인자를 넘겨주고 있습니다. 최종적으로 execute() 함수를 AsyncTask를 실행합니다.

public class MyAsyncTask extends AsyncTask<Void, Integer, Boolean> {

    int value;
    TextView textView;
    ProgressBar progressBar;

    public MyAsyncTask(int value, TextView textView, ProgressBar progressbar)
    {
        this.textView = textView;
        this.progressBar = progressbar;
        this.value = value;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressBar.setMax(value);
        progressBar.setProgress(0);
    }

    @Override
    protected Boolean doInBackground(Void... strings){

        for(int i=0; i<= value; i++)
        {
            publishProgress(i);
        }

        return true;
    }

    @Override
    protected void onPostExecute(Boolean s) {
        super.onPostExecute(s);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        progressBar.setProgress(values[0].intValue());
        textView.setText(values[0].toString());

        super.onProgressUpdate(values);
    }

    @Override
    protected void onCancelled(Boolean s) {
        super.onCancelled(s);
    }
}

▼ onPreExecute() 함수에서는 setMax()를 통해 ProgressBar의 작업 진행 정도를 표시할 Max 값을 지정합니다. 해당 값은 EditText에서 사용자가 입력한 데이터가 들어갑니다. 그다음으로 setProgress() 함수를 통해서 ProgressBar의 최초 값을 지정합니다. 

 

▼ doInBackground() 함수에서는 지정된 Max Value까지 반복문을 실행합니다. 

이때 ProgressBar의 진행 정도를 표현하기 위해 UI 갱신이 이루어지기 때문에 publishProgress() 함수를 호출하여 UI 업데이트를 위한 작업을 진행합니다. publishProgress() 함수를 호출하면 onProgressUpdate() 함수에 UI 업데이트 관련 처리를 진행하면 되는데 해당 예제에서는 setProgress()를 통해 현재 ProgressBar의 값을 지정합니다.

 

List of Articles
번호 제목 날짜 조회 수
237 안드로이드 - RecyclerView 안에 RecyclerView 구현하기 file 2021.04.02 501
236 안드로이드 - Json 파싱하기 file 2021.04.02 825
235 안드로이드 - 타이머(Timer) 구현하기 2021.04.01 1365
234 안드로이드 - SQLiteDatabase 구현하기 file 2021.04.01 241
233 안드로이드 - 리사이클러 뷰(RecyclerView) 구현 file 2021.04.01 387
232 안드로이드 - 네비게이션 드로어(Navigation Drawer)를 활용하여 슬라이드 메뉴 구현하기 file 2021.04.01 1844
231 안드로이드 - 툴바(ToolBar)를 사용하여 앱바(App Bar) 구현하기 file 2021.04.01 444
» 안드로이드 - 프로그레스바(ProgressBar) 구현하기 file 2021.04.01 447
229 안드로이드 - AsyncTask 구현 예제 file 2021.04.01 280
228 안드로이드 - 액티비티(Activity)와 액티비티 생명 주기(Activity Life Cycle) file 2021.04.01 225
227 안드로이드 - 리스트뷰(ListView) 구현 file 2021.04.01 488
226 안드로이드 - 컨텍스트 메뉴(Context Menu) 사용 예제 file 2021.04.01 205
225 안드로이드 - 옵션 메뉴 (Option Menu) 구현 방법 file 2021.04.01 282
224 안드로이드 - 명시적 인텐트(Explicit Intent)와 암시적 인텐트 (Implicit Intent) file 2021.04.01 324
223 안드로이드 - 날짜 및 시간 정보 입력받기 (DatePickerDialog / TimePickerDialog) file 2021.04.01 1754
222 안드로이드 - setContentView()와 레이아웃 전개자(LayoutInflater) 2021.04.01 227
221 안드로이드 - AlertDialog 사용하여 알림창 띄우는 방법 총정리 file 2021.03.31 680
220 안드로이드 - SnackBar를 사용해 팝업창 띄우기 file 2021.03.31 277
219 안드로이드 - 토스트(Toast) 메시지 사용하기. file 2021.03.31 321
218 안드로이드 - 액티비티(Activity)로부터 결과 데이터 받아오기 file 2021.03.31 481
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved