이 예제는 Kotlin을 사용하여 Android에서 ImageView(이미지뷰)를 지우는 방법을 단계별로 소개합니다. 버튼을 클릭하면 화면에 표시된 이미지가 사라지고 토스트 메시지가 나타나는 간단한 앱을 만들어 보겠습니다.
핵심 개념
ImageView에 설정된 배경 이미지를 제거하려면 setBackgroundDrawable(null) 메서드에 null을 전달하면 됩니다. 참고로 이 메서드는 현재 deprecated(사용 중단 권고) 상태이므로, 최신 프로젝트에서는 imageView.setBackground(null) 또는 imageView.setImageDrawable(null)을 사용하는 것이 좋습니다.
1단계 — 새 프로젝트 만들기
Android Studio에서 File → New Project 메뉴로 이동한 뒤, 새 프로젝트 생성에 필요한 정보를 모두 입력하여 새 프로젝트를 만듭니다.
2단계 — 레이아웃 XML 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다. 화면 하단에는 버튼 하나, 그 위를 채우는 ImageView 하나로 구성됩니다.
예제 코드
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:onClick="clickHere"
android:text="클릭하세요" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/button"
android:layout_centerInParent="true"
android:background="@drawable/image" />
</RelativeLayout>
3단계 — MainActivity.kt 작성
src/MainActivity.kt 파일에 아래 코드를 추가합니다. 버튼을 클릭하면 clickHere() 메서드가 호출되어 ImageView의 배경을 null로 설정하고 토스트 메시지를 표시합니다.
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
lateinit var imageView: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
imageView = findViewById(R.id.imageView)
}
fun clickHere(view: View) {
imageView.setBackgroundDrawable(null)
Toast.makeText(this, "이미지가 지워졌습니다", Toast.LENGTH_SHORT).show()
}
}
4단계 — AndroidManifest.xml 확인
AndroidManifest.xml 파일에 아래와 같이 메인 액티비티가 올바르게 등록되어 있는지 확인합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
앱 실행하기
이제 애플리케이션을 실행해 보겠습니다. 실제 Android 스마트폰이 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 열고, 툴바에서 실행(Run) 아이콘
을 클릭합니다. 기기 선택 목록에서 사용 중인 모바일 기기를 고르면, 해당 기기 화면에 앱의 기본 화면이 표시됩니다.


실행 결과
'클릭하세요' 버튼을 누르면 ImageView의 이미지가 즉시 사라지고, 화면 하단에 '이미지가 지워졌습니다'라는 토스트 메시지가 잠시 나타납니다. 이처럼 배경 drawable에 null을 설정하는 것만으로도 ImageView를 손쉽게 초기화할 수 있습니다.