【Kotlin入門】Androidでボタンをクリックして新しいアクティビティを起動する方法
このチュートリアルでは、Kotlinを使用してAndroidアプリでボタンをクリックした際に、新しいアクティビティ(画面)を起動する方法を解説します。画面遷移にはIntent(インテント)を使用します。
ステップ1:新規プロジェクトの作成
Android Studioで「File」→「New Project」を選択し、必要な項目を入力して新しいプロジェクトを作成します。使用言語は「Kotlin」を選択してください。
ステップ2:activity_main.xml の編集
res/layout/activity_main.xml に以下のコードを追加します。画面中央にボタンを配置したシンプルなレイアウトです。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="TutorialsPoint"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:color/background_dark"
android:padding="4dp"
android:text="Open New Activity"
android:textColor="@color/colorAccent"
android:textSize="32sp"
android:textStyle="bold" />
</RelativeLayout>
ステップ3:MainActivity.kt の編集
src/MainActivity.kt に以下のコードを記述します。
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button: Button = findViewById(R.id.button)
button.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
}
}
コードのポイント
findViewById()でレイアウト上のボタンを取得します。setOnClickListener { }の中に、クリックされたときの処理を記述します。Intent(this, SecondActivity::class.java)で遷移先を指定し、startActivity()を呼び出すことで新しいアクティビティが起動します。
ステップ4:新しいアクティビティの作成
「SecondActivity」を新規作成し、各ファイルに以下のコードを追加します。
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="4dp"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="TutorialsPoint"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Welcome to Second Activity"
android:textAlignment="center"
android:textColor="@android:color/holo_blue_dark"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
SecondActivity.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
}
}
ステップ5:AndroidManifest.xml への登録
作成したアクティビティをマニフェストファイルに登録します。Android Studioから新規アクティビティを追加した場合は、この記述は自動的に挿入されます。
<?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」アイコン
をクリックします。接続したデバイスを選択すると、スマートフォンにアプリが起動します。
ボタンをタップすると、次のように2つ目のアクティビティへ遷移します。


-
【Android】アクティビティで戻るボタンを処理する方法をわかりやすく解説
本記事では、Androidアプリのアクティビティにおいて戻るボタン(バックキー)を処理する方法を、実際のサンプルコードとともにステップ形式で解説します。手順1:新規プロジェクトを作成するまずはAndroid Studioで新しいプロジェクトを作成しましょう。「File」⇒「New Project」を選択し、必要な項目をすべて入力してプロジェクトのセットアップを完了させてください。手順2:レイアウトファイル(activity_main.xml)を編集するres/layout/activity_main.xml に以下のコードを追加します。中央にテキストとボタンを配置するシンプルなレイアウトです。
-
AndroidのActivity.finish()とは?仕組みと使い方をサンプルコードで解説
Activity.finish()とは Androidアプリ開発では、Activity.finish()を呼び出すことで、現在表示中のアクティビティを終了させることができます。finish()が実行されると、そのアクティビティはバックスタックから取り除かれ、システムによって破棄(onDestroy)されます。その結果、ユーザーが「戻る」ボタンを押しても、終了済みの画面には戻れなくなります。 この記事では、実際にサンプルプロジェクトを構築しながら、finish()がどのように動作するのかをステップごとに確認していきます。 手順1:新規プロジェクトを作成する Android Studioを起動し