Kotlinを使って、Androidアプリの画面にボタンを表示する方法と、ボタンが押された時に処理を実行する方法について説明します。 ボタン表示には、ButtonクラスもしくはButtonタグを用います。
java/com/example/MainActivity.kt
package com.example.sample
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
class MainActivity : Activity() {
private val clickListener : View.OnClickListener = View.OnClickListener {
Toast.makeText(applicationContext, "ボタンが押されました", Toast.LENGTH_SHORT).show();
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button: Button = findViewById(R.id.button)
button.setOnClickListener(clickListener)
}
}
res/layout/activity_main.xml(レイアウトXML)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:text="@string/label"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
res/values/strings.xml
<resources>
<string name="label">ボタン</string>
</resources>


目次
前提条件
今回説明する手順は、以下を前提に説明します。
- 「Kotlin」のコードを読めること。
- 「Android Studio プロジェクト新規作成 (Windows編)」が実施されていること。
動作確認端末
- Android Studio 3.3
- Nexus 5X API 28(Android9.0) – Androidエミュレータ
Buttonの概要
- Buttonは、画面にボタンを表示するためのAPIです。
- Buttonは、クラスもしくは(レイアウトXMLで利用できる)タグとして利用することができます。
- Buttonクラスは、TextViewを継承しているためTextViewのメソッドを利用することができます 。
- Buttonタグは、TextViewタグで利用可能な属性を利用することができます 。
Buttonの使い方
ボタンを表示する(Buttonタグ)
- Buttonタグのandroid:text属性の値には、ボタンラベルの文字内容を設定します。
- 文字内容の設定には、strings.xmlに定義したリソースIDを用います。
- 例えば、以下のように文字内容をstrings.xmlに定義します。
strings.xmlで文字内容を定義
<resources>
<string name="label">ボタン</string>
</resources>
- 次にandroid:text属性に対して、定義した文字のリソースIDを以下のように記述します。
ボタンに文字を表示する
<Button
android:id="@+id/button"
android:text="@string/label"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
- すると以下のようにボタンが表示されます。

- android:text属性には、表示したい文字列を直接記述することもできます。
android:text属性に文字列を直接記述することもできる
android:text="ボタン"
表示したい文字列をandroid:text属性に直接記述することは非推奨ですので、
このような記述方法はなるべく避けるべきでしょう。
ボタンの文字を設定する(Buttonクラス)
- Buttonクラスを使って、ボタンの文字を設定するためにはいくつかの方法があります。
- 1つ目の方法は、textプロパティを使う方法です。
- まずはレイアウトXMLで定義したButtonのオブジェクトを取得します。
文字設定対象のButtonを取得する
setContentView(R.layout.activity_main)
val button: Button = findViewById(R.id.button)
- 次に、さきほど取得したButtonオブジェクトのtextプロパティに表示したい文字列を設定します。
textプロパティを使って文字表示する
button.text = "ボタン2"
- すると以下のようにボタンが表示されます。

- 2つ目の方法は、setText()メソッドを使う方法です。
- setText()メソッドには、表示したい文字列のリソースIDを引数として渡します。
setText()メソッドを使って文字表示する
button.setText(R.string.label)
- すると以下のように文字が表示されます。

-
ボタンの文字を後から変更したい場合(※)は、Buttonクラスを用いた方法を利用するとよいでしょう。
- ※ 何かしらの処理実行後、ボタンの状態を変更したい場合など
ボタンが押された時の処理を実行する
- ボタンが押された時の処理を定義したい場合は、setOnClickListener()メソッドを使用します。
- 引数はView.OnClickListener型で、ボタン押下時の処理を定義したオブジェクトを渡します。
- 以下の例は、ボタン押下時にトーストが画面に表示されるコードです。
ボタンが押された時にトーストを表示する
package com.example.sample
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
class MainActivity : Activity() {
private val clickListener : View.OnClickListener = View.OnClickListener {
Toast.makeText(applicationContext, "ボタンが押されました", Toast.LENGTH_SHORT).show();
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button: Button = findViewById(R.id.button)
button.setOnClickListener(clickListener)
}
}
- View.OnClickListener型のclickListenerプロパティを宣言し、その中でボタンが押された時の処理を記述します。
ボタンが押された時の処理を記述している
private val clickListener : View.OnClickListener = View.OnClickListener {
Toast.makeText(applicationContext, "ボタンが押されました", Toast.LENGTH_SHORT).show();
}
- 実際にボタンが押された時に処理を実行するためには、押されるボタンであるButtonオブジェクトに、View.OnClickListenerオブジェクトをリスナーとして設定する必要があります。
ボタンオブジェクトを取得
val button: Button = findViewById(R.id.button)
button.setOnClickListener(clickListener)
- ボタンを押下すると、以下のようにトーストが表示されます。

参考情報
本リファレンス内容は、以下を参考にしています。
- View | Android Developers(https://developer.android.com/reference/kotlin/android/view/View)
- Button | Android Developers(https://developer.android.com/reference/kotlin/android/widget/Button)