본문 바로가기
안드로이드 웹앱 콘테츠 개발자 양성(국비지원)/Thread

Android Studio Thread 4

by 차누감 2019. 9. 19.

ProgressDialog

1)Wheel

2)Bar

예제를 만들어보자

activity_main.xml부터 코드 작성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="wheel type pogress"
        android:textAllCaps="false"
        android:onClick="clickBtn"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Bar type pogress"
        android:textAllCaps="false"
        android:onClick="clickBtn2"/>
 
 
</LinearLayout>
 

디자인된 모습

 

MainActivity.java 코드 작성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 
import androidx.annotation.NonNull;
 
 
public class MainActivity extends AppCompatActivity {
 
    ProgressDialog dialog;
    int gauge =0;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    public void clickBtn(View view) {
        if(dialog!=nullreturn;
 
        //wheel type progress dialog
        dialog= new ProgressDialog(this);
        dialog.setTitle("wheel dialog");
        dialog.setMessage("downloading ...");
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
 
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
 
        //3초 뒤에 dialog 종료(dismiss)
        handler.sendEmptyMessageDelayed(0,3000);
 
    }
 
    Handler handler= new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(dialog!=null){
                dialog.dismiss();
                dialog=null;
            }
 
            super.handleMessage(msg);
        }
    };
    public void clickBtn2(View view) {
        if(dialog!=null)return;
 
        //bar type Progress dialog
        dialog= new ProgressDialog(this);
        dialog.setTitle("막대바 다이얼로그");
        dialog.setMessage("다운로드 중.....");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
 
        dialog.setMax(100); //맥스 값을 지정할 수 있다. 안쓰면 기본 100
 
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
 
        dialog.setProgress(gauge); //처음 게이지바 시작 지점
 
        new Thread(){
            @Override
            public void run() {
                gauge=0;
 
                while (gauge<100){
                    gauge++;
                    dialog.setProgress(gauge);
 
                    // 0.05초 대기 (딜레이가 없으면 너무 빨리 지나감)
 
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) { e.printStackTrace();}
                }
                dialog.dismiss();
                dialog=null;
            }
        }.start();
 
    }
 
}
 
 
 

Wheel Dialog와 Bar Dialog는 5초 후에 종료되게 된다.

댓글