KotlinでAndroidアクティビティ間にドローアブル(Drawable)を渡す方法【コード例付き】
はじめに
この記事では、Kotlinを使用してAndroidアプリのアクティビティ間でドローアブル(Drawable)を受け渡す方法を、実際のコード例とともに解説します。
ポイントは、ドローアブルオブジェクトそのものではなく、リソースID(int値)をIntentのExtraとして渡す点です。これにより、シンプルかつ安全に画像を別のアクティビティへ引き継ぐことができます。
ステップ1:新規プロジェクトを作成する
Android Studioを開き、「File」→「New Project」を選択して、必要事項を入力し新しいプロジェクトを作成します。使用言語は「Kotlin」を選択してください。
ステップ2:activity_main.xml を編集する
res/layout/activity_main.xml に以下のコードを追加します。画面中央にボタンを1つ配置し、タップすると2番目のアクティビティへ遷移する構成です。
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="Tutorials Point" android:textAlignment="center" android:textColor="@android:color/holo_green_dark" android:textSize="32sp" android:textStyle="bold" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Transfer Bitmap to 2nd Activity" /> </RelativeLayout>
ステップ3:MainActivity.kt を実装する
src/MainActivity.kt に以下のコードを記述します。ボタンがクリックされると、IntentにドローアブルのリソースIDを「picture」というキーで格納し、NewActivityを起動します。
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val button: Button = findViewById(R.id.button)
button.setOnClickListener {
val intent = Intent(this@MainActivity, NewActivity::class.java)
intent.putExtra("picture", R.drawable.ic_baseline_directions_walk_24)
startActivity(intent)
}
}
}
ステップ4:NewActivity を作成する
新しいアクティビティ「NewActivity」を作成し、以下のコードを追加します。
NewActivity.kt
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
class NewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new)
title = "KotlinApp"
val imageView: ImageView = findViewById(R.id.imageView)
val bundle = intent.extras
if (bundle != null) {
val picture = bundle.getInt("picture")
imageView.setImageResource(picture)
}
}
}
受け取り側では、intent.extras から「picture」キーに対応するint値(リソースID)を取得し、setImageResource() メソッドでImageViewに表示しています。
activity_new.xml
<?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=".NewActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="25sp" android:text="Second Activity" android:textSize="16sp" android:textStyle="bold" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout>
ステップ5: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スマートフォンをPCに接続済みであることを前提とします。Android Studioでプロジェクト内のアクティビティファイルを開き、ツールバーの「Run」アイコン
をクリックしてください。接続したモバイルデバイスを選択すると、端末に初期画面が表示されます。

ボタンをタップすると2番目のアクティビティへ遷移し、渡されたドローアブルがImageViewに正しく表示されます。

-
Androidでアクティビティ間にドローアブルを渡す方法【サンプルコード付きで解説】
この記事では、Androidアプリにおいて2つのアクティビティ間でドローアブル(画像リソース)を受け渡す方法を、実際に動作するサンプルコードとともに段階的に解説します。実装のポイント:ドローアブル本体ではなく「リソースID」を渡すドローアブルオブジェクトをIntentに直接渡そうとすると、データサイズが大きすぎてエラーになる場合があります。そこで本記事では、ドローアブルのリソースID(int値)を putExtra() で渡し、受け取り側で setImageResource() を使って表示するというシンプルかつ確実な手法を採用します。これが画像リソースの受け渡しにおけるベストプラクティスです
-
Androidで複数のアクティビティ(画面)を切り替える方法【サンプルコード付き】
本記事では、Androidアプリで複数の画面(アクティビティ)をIntentを使って切り替える方法を、サンプルコードとともにわかりやすく解説します。ボタンをタップすると「MainActivity」から「SecondActivity」へ遷移し、そこから再び元の画面へ戻れるというシンプルな例です。 手順1:Android Studioで新規プロジェクトを作成する Android Studioを開き、メニューの「File」→「New Project」を選択して、必要な項目を入力のうえ新しいプロジェクトを作成します。 手順2:activity_main.xmlにコードを追加する メイン画面のレイアウト