안드로이드

[Android Kotlin] 안드로이드 코틀린 Progress Bar

행복하개! 2020. 3. 21. 02:32
<?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/pb1"
        style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Large"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/pb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ProgressBar
        android:id="@+id/pb3"
        style="@android:style/Widget.DeviceDefault.ProgressBar.Small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/pb4"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5만큼 증가" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5만큼 증가" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="50으로 설정" />

</LinearLayout>
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 별다른 리스너가 존재하지 않는다.

        btn.setOnClickListener { view ->
            pb4.incrementProgressBy(5)
        }

        btn2.setOnClickListener { view ->
            pb4.incrementProgressBy(-5)
        }

        btn3.setOnClickListener { view ->
            pb4.progress = 50
        }
    }
}

 

 

 

자바에서도 마찬가지이지만 ProgressBar는 별 다른 리스너가 존재하지 않는다. incrementProgressBy()를 통해서 증감을 할 수 있다. 아예 progress에 값을 넣을 수도 있다. 상황에 맞게 사용하면 된다.

 

궁금하신 점은 덧글로 적어주세요.