こちらと同様のことを、Ubuntu 23.04 にインストールした Android Studio Giraffe | 2022.3.1 で行いました。
Let’s Build Your First Android App in Kotlin (Android Studio Giraffe)
Empty Views Activity を選ぶ
Language は Kotlin にする
activity_main.xml を次のようにする
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://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:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt を次のようにする
MainActivity.kt
package com.example.n003
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
private val list = listOf("東京", "新橋", "品川", "川崎", "横浜", "戸塚", "大船")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button: Button = findViewById(R.id.button)
button.setOnClickListener {
val index: Int = Random.nextInt(list.size)
val randomDogName = list[index]
Toast.makeText(this,randomDogName, Toast.LENGTH_SHORT).show()
button.text = randomDogName
}
}
}
Run
Build APK(s)
app-debug.apk を USB などで、実機に持って行って、インストールします。
つまり、app-debug.apk はインストーラーです。