본문 바로가기
DEVELOPMENT

[실무에 바로 적용하는 안드로이드 프로그래밍 - Kotlin] Challenge 2: Previous Button & Image Button

by 200% 2021. 7. 16.

1. Previous 버튼 추가

(1) xml에 버튼 추가

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/previous_button"
            android:drawableEnd="@drawable/arrow_left"
            android:drawablePadding="4dp"
            android:layout_marginRight="10dp"
            android:text="@string/previous_button"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/next_button"
            android:drawableEnd="@drawable/arrow_right"
            android:drawablePadding="4dp"
            android:text="@string/next_button"/>
    </LinearLayout>

(2) 코틀린에 다음 문장들 각각 알맞은 위치에 추가

private lateinit var prevButton: Button

prevButton = findViewById(R.id.previous_button)

prevButton.setOnClickListener {
            currentIndex = (currentIndex - 1 + questionBank.size) % questionBank.size
            updateQuestion()
        }

 

2. Button을 ImageButton으로 변경

(1) xml 변경

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/previous_button"
            android:src="@drawable/arrow_left"
            android:layout_marginRight="10dp"
            android:contentDescription="@string/previous_button"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/next_button"
            android:src="@drawable/arrow_right"
            android:contentDescription="@string/next_button"/>
</LinearLayout>

(2) 코틀린 변경

private lateinit var nextButton: ImageButton
private lateinit var prevButton: ImageButton