어떤 작업의 진행상황을 보여줄 때, 작업이 얼마나 남았는지 확인할 때에 사용하는
ProgressBar의 사용방법을 알아보자.
제일 먼저 ProgressBar의 2가지 형태를 알고 가야 한다.
불확정적(indeterminate) 상태 / 확정적(determinate) 상태이다.
먼저 indeterminate 의 경우 작업의 진행상황이 불명확하고 막연할 때 사용하는 것으로
ProgressBar에 기본적으로 적용되어 있으므로 값을 따로 지정하지 않으면 기본적으로 indeterminate 이다.
determinate 는 작업의 진행단계가 명확하고, 끝이 명확한 경우에 사용한다.
ProgressBar의 스타일을 만들 때 max 값을 지정함으로써 설정할 수 있다.
아래는 determinate 의 예시 코드이다.
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:max="10"/>
이제 activity 파일에서 작업을 진행할 때 진행 상황을 반영해줄 때는
setProgress 메소드를 사용하면 된다.
ProgressBar progressBar = findViewById(R.id.progressBar);
progressBar.setProgress(X);
실제 코드 사용 예제는 다음과 같다.
퀴즈 10문제를 풀면서 버튼을 누를 때마다 한 문제씩 진행되면서, 진행상황을 보여주는 코드이다.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1. 화면 UI와 변수를 매칭 !!!! 프로그레스바 연결!!!!
txtQuiz = findViewById(R.id.txtQuiz);
txtResult = findViewById(R.id.txtResult);
btnTrue = findViewById(R.id.btnTrue);
btnFalse = findViewById(R.id.btnFalse);
ProgressBar progressBar = findViewById(R.id.progressBar);
setQuizData();
txtQuiz.setText( quizArrayList.get(quizIndex).question );
btnTrue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean answer = false;
if(quizIndex < quizArrayList.size()){
answer = quizArrayList.get(quizIndex).answer;
} else {
return ;
}
if (answer == true) {
txtResult.setText("정답입니다.");
count = count + 1;
} else{
txtResult.setText("틀렸습니다.");
}
// !!!버튼을 누르고 quizIndex에 하나씩 진행상황이 저장될 때에 setProgress 메소드 사용!!!
quizIndex = quizIndex + 1;
if (quizIndex < quizArrayList.size()){
txtQuiz.setText( quizArrayList.get(quizIndex).question );
progressBar.setProgress(quizIndex);
} else {
txtQuiz.setText("10문제 모두 해결하셨습니다.");
txtResult.setText("맞춘 문제는 " + count + " 개 입니다.");
progressBar.setProgress(quizIndex);
}
}
});
btnFalse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean answer = false;
if(quizIndex < quizArrayList.size()){
answer = quizArrayList.get(quizIndex).answer;
} else {
return;
}
if (answer == false) {
txtResult.setText("정답입니다.");
count = count + 1;
} else{
txtResult.setText("틀렸습니다.");
}
// !!!버튼을 누르고 quizIndex에 하나씩 진행상황이 저장될 때에 setProgress 메소드 사용!!!
quizIndex = quizIndex + 1;
if (quizIndex < quizArrayList.size()){
txtQuiz.setText( quizArrayList.get(quizIndex).question );
progressBar.setProgress(quizIndex);
} else {
txtQuiz.setText("10문제 모두 해결하셨습니다.");
txtResult.setText("맞춘 문제는 " + count + " 개 입니다.");
progressBar.setProgress(quizIndex);
}
}
});
}
'Android' 카테고리의 다른 글
Android Studio - 안드로이드 네트워크 통신을 위한 Volley 라이브러리 / AndroidManifest.xml 파일 설정법 (0) | 2022.07.11 |
---|---|
Android Studio - LifeCycle 라이프사이클 (0) | 2022.07.11 |
Android Studio - TextView의 setText 함수에서 쉽게 숫자를 문자열로 변경하기 (0) | 2022.07.08 |
Android Studio - Linear Layout의 종류와 layout_weight (0) | 2022.07.08 |
Android Studio - UI의 TextView / Button의 속성 (0) | 2022.07.08 |
댓글