관리 메뉴

Welcome! Everything is fine.

[Android] ν”„λ‘œκ·Έλ ˆμŠ€λ°” λ³Έλ¬Έ

Android

[Android] ν”„λ‘œκ·Έλ ˆμŠ€λ°”

개발곰발 2022. 3. 6.
728x90

πŸ“Œ ν”„λ‘œκ·Έλ ˆμŠ€λ°”(ProgressBar)

  • ν”„λ‘œκ·Έλ ˆμŠ€λ°” : μž‘μ—…μ˜ 진행 정도λ₯Ό ν‘œμ‹œν•˜μ—¬ μ‚¬μš©μžμ—κ²Œ μ•Œλ €μ€€λ‹€. λŒ€ν‘œμ μΈ ν”„λ‘œκ·Έλ ˆμŠ€λ°”λŠ” λ§‰λŒ€ λͺ¨μ–‘κ³Ό 원 λͺ¨μ–‘ 두 가지 ν˜•νƒœκ°€ μžˆλ‹€. νŒ”λ ˆνŠΈμ˜ Widgetsμ—μ„œ μ°Ύμ•„ λ„£λŠ”λ‹€.
속성 μ„€λͺ…
λ§‰λŒ€ λͺ¨μ–‘ μž‘μ—…μ˜ 진행 정도λ₯Ό λ§‰λŒ€λͺ¨μ–‘μœΌλ‘œ ν‘œμ‹œν•œλ‹€. style 속성 값을 '?android:attr/progressBarStyleHorizontal'둜 μ„€μ •ν•œλ‹€. 
원 λͺ¨μ–‘ μž‘μ—…μ˜ 진행 정도λ₯Ό 원 λͺ¨μ–‘μ˜ ν”„λ‘œκ·Έλ ˆμŠ€λ°”λ‘œ ν‘œμ‹œν•œλ‹€.
  • <ProgressBar> νƒœκ·Έ : XML λ ˆμ΄μ•„μ›ƒμ— ν”„λ‘œκ·Έλ ˆμŠ€λ°”λ₯Ό μΆ”κ°€ν•  λ•Œ μ‚¬μš©λœλ‹€.
    • max 속성 : ν”„λ‘œκ·Έλ ˆμŠ€λ°”κ°€ κ°–λŠ” μ΅œλŒ€ λ²”μœ„λ₯Ό μ„€μ •ν•  λ•Œ μ‚¬μš©
    • progress 속성 : ν”„λ‘œκ·Έλ ˆμŠ€λ°”μ˜ ν˜„μž¬ 값을 μ„€μ •ν•  λ•Œ μ‚¬μš©

βœ” ν”„λ‘œκ·Έλ ˆμŠ€λ°”μ˜ ν˜„μž¬ 값을 λ°”κΏ€ λ•Œ μ‚¬μš©ν•˜λŠ” λŒ€ν‘œ λ©”μ„œλ“œ

void setProgress(int progress)

→ μ •μˆ˜ 값을 λ°›μ•„ ν”„λ‘œκ·Έλ ˆμŠ€λ°”μ˜ ν˜„μž¬ κ°’μœΌλ‘œ μ„€μ •ν•  λ•Œ μ‚¬μš©ν•œλ‹€.

void incrementProgressBy(int diff)

→ ν˜„μž¬ μ„€μ •λ˜μ–΄ μžˆλŠ” 값을 κΈ°μ€€μœΌλ‘œ 값을 λ”ν•˜κ±°λ‚˜ λΊ„ λ•Œ μ‚¬μš©ν•œλ‹€. 

 

βœ” 타이틀바에 ν”„λ‘œκ·Έλ ˆμŠ€λ°” ν‘œμ‹œν•˜λŠ” λ©”μ„œλ“œ

requestWindowFeature(Window.FEATURE_PROGRESS);

βœ” μ‹€μŠ΅ λ‚΄μš©

public class MainActivity extends AppCompatActivity {
    ProgressDialog dialog;

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

        ProgressBar progressBar = findViewById(R.id.progressBar);
        progressBar.setIndeterminate(false);
        progressBar.setProgress(20);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                dialog = new ProgressDialog(MainActivity.this);
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setMessage("데이터λ₯Ό ν™•μΈν•˜λŠ” μ€‘μž…λ‹ˆλ‹€.");

                dialog.show();
            }
        });

        Button button2 = findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        });
    }
}

앱을 μ‹€ν–‰ν•˜λ©΄ λ‹€μŒκ³Ό 같은 화면이 λ‚˜μ˜¨λ‹€. 보여주기 λ²„νŠΌμ„ λˆ„λ₯΄λ©΄ ν”„λ‘œκ·Έλ ˆμŠ€λ°” λŒ€ν™”μƒμžκ°€ ν‘œμ‹œλœλ‹€. progressDialog 객체λ₯Ό λ§Œλ“€κ³  κ·Έ μŠ€νƒ€μΌμ„ μ–΄λ–»κ²Œ μ •ν•˜λŠλƒμ— 따라 λͺ¨μ–‘이 λ‹¬λΌμ§€λŠ” 것을 μ•Œ 수 μžˆλ‹€.

setProgress(80)(쒌) / setProgress(20)(우)
ProgressDialog.STYLE_HORIZONTAL(쒌) / ProgressDialog.STYLE_SPINNER(우)