メモ > 技術 > IDE: AndroidStudio > アプリの作成(XMLレイアウト)
アプリの作成(XMLレイアウト)
■ハローワールド
src/main/java/org/refirio/helloworld/MainActivity.kt
package org.refirio.helloworld
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
src/main/res/layout/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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ハローワールド!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
■ボタンとトースト
src/main/java/org/refirio/helloworld/MainActivity.kt
package org.refirio.helloworld
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val testButton = findViewById<Button>(R.id.test_button)
testButton.setOnClickListener {
Toast.makeText(applicationContext, "これはトーストです", Toast.LENGTH_SHORT).show();
}
}
}
src/main/res/layout/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/test_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ボタン"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
■ビューバインディング
AndroidStudio4.1から、ビューバインディングという機能が用意されている。
これはfindViewByIdの後継となる機能で、findViewByIdよりも高速に動作するらしい。
build.gradle(Androidビューでは「Gradle Scripts」直下の「build.gradle (Module: helloworld.app)」が該当する)
android {
〜略〜
buildFeatures {
viewBinding = true
}
}
コードを変更したら「Sync Now」をクリック。
さらに、アクティビティを以下のように変更。
src/main/java/org/refirio/helloworld/MainActivity.kt
package org.refirio.helloworld
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import org.refirio.helloworld.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.testButton.setOnClickListener {
Toast.makeText(applicationContext, "これはトーストです", Toast.LENGTH_SHORT).show();
}
}
}
これで先と同じ結果が得られる。
ビューバインディングでは、レイアウトXMLファイルに対応するクラスが自動で生成される。
今回の場合レイアウトファイルは activity_main.xml なので、これに接尾語「Binding」を加えた「ActivityMainBinding」が自動的に生成されている。
このクラスを使うように指定する。そしてonCreateメソッド内で
・生成されたバインディングクラスに含まれるinflateメソッドを呼び出す。
・rootプロパティからルートビューへの参照を取得する。
・取得したルートビューをsetContentViewにわたす。
という処理を行う。(これはビューバインディングを使う場合のお決まりの処理となる。)
「testButton」は、ボタンのID「test_button」をキャメルケースにしたものを指定するみたい。
■画像の表示
任意の画像をコピーする。
プロジェクトウインドウのツリーで「res」内の「drawable」を選択する。
その状態でペーストを行う。
コピー先の選択として「drawable」と「drawable-24」のように表示されるが、「drawable」にペーストする。
(「drawable-24」はAPI24(Android7.0)以降のバージョンでのみ使用したいリソースを入れる場所。)
コピーダイアログが開くので、そのまま「Refactor」をクリックする。
これで「drawable」フォルダ内に画像が表示される。
レイアウトのXMLを開き、「common」内の「ImageView」をドラッグ&ドロップで配置する。
画像の選択ダイアログが表示されるので、配置したい画像を選択して「OK」をクリック。
画像が配置されるので、あとはテキストやボタンと同様に表示位置の調整を行う。
■アクティビティの追加とインテント
プロジェクトウインドウのツリーで「app」を右クリックし、
「New → Activity → Empty Activity」を選択する。
表示されたダイアログで「Activity Name」で「ResultActivity」と入力して、あとはデフォルトのまま「Finish」ボタンを押す。
プロジェクトウインドウのツリーに「ResultActivity」と「activity_result.xml」が追加される。
以下のようにすると、追加したアクティビティに遷移するためのボタンとして機能する。
src/main/java/org/refirio/helloworld/MainActivity.kt
binding.testButton.setOnClickListener {
val intent = Intent(this, ResultActivity::class.java)
startActivity(intent)
}
■staticメソッドを定義する
companion object を使ってstaticメソッドを定義する。
Kotlinで静的変数・メソッドを定義する方法 - goroyaのSE日記
http://gogoroya.hatenadiary.jp/entry/2017/06/05/234348
kotlinはJavaからどう見えるか? - Qiita
https://qiita.com/boohbah/items/167233c7eafe17f3150b
■Kotlin RPEL
※XcodeでいうPlaygroundのようなもの。
プロジェクトを開いた状態で、メニューの「ツール → Kotlin → Kotlin RPEL」から実行できる。
■アプリのアイコンを変更する
※未検証。
Android Studio 開発【アプリアイコンの変更】 - ハコニワ デザイン
http://hakoniwadesign.com/?p=4908
Androidアプリのアイコン画像を変更する方法【初心者向け】 | TechAcademyマガジン
https://techacademy.jp/magazine/2710
■スプラッシュ画面を表示する
※未検証。
Splash画面でpostDelayedして一定時間画面を表示する - Qiita
https://qiita.com/shanonim/items/35296c02494ffdbd7273
Androidでスプラッシュ画面を作る方法 - Qiita
https://qiita.com/glayash/items/646e5c0d5de82cfc17bc
■カメラ
【Android】カメラ機能に触れてみる(Android5.0〜) - vaguely
https://mslgt.hatenablog.com/entry/2015/05/12/013013
AndroidのCamera2 APIのサンプル : 時々、失業SEの開発日誌
http://blog.kotemaru.org/2015/05/23/android-camera2-sample.html
【kotlin】CameraXでAndroidカメラを実装してみた | RE:ENGINES
https://re-engines.com/2019/10/31/%E3%80%90kotlin%E3%80%91camerax%E3%81%A7android%E3%82%AB%E3%83%A1%...
Getting Started with CameraX
https://codelabs.developers.google.com/codelabs/camerax-getting-started/#2
【Android】CameraX試してみた - Qiita
https://qiita.com/emusute1212/items/6195cf18bfcbea2ef1d1
Android CameraXでプレビューを表示する | Developers.IO
https://dev.classmethod.jp/smartphone/android/android-camerax-preview/
Android4と5・6でカメラは仕様が大きく変わっているみたい。
以下、2021年6月時点で改めて調べたときの記事。(要検証。)
CameraX の概要 | Android デベロッパー | Android Developers
https://developer.android.com/training/camerax?hl=ja
プレビューを実装する | Android デベロッパー | Android Developers
https://developer.android.google.cn/training/camerax/preview?hl=ja
【初心者向け】CameraXでAndroidのカメラアプリを作る - Qiita
https://qiita.com/senju797/items/7b846fce6a828004279c
AndroidのカメラサポートライブラリのCameraXを使ってみました | Kotlin | アプリ関連ニュース | ギガスジャパン
http://www.gigas-jp.com/appnews/archives/9555
AndroidでCameraXを使ってみる(プレビューの表示) - くらげになりたい。
https://www.memory-lovers.blog/entry/2021/01/25/230000
■プッシュ
Androidのプッシュ機能は、以下のように仕組みが変わっている。
C2DM(Cloud to Device Messaging)
↓
GCM(Google Cloud Messaging)
↓
FCM(Firebase Cloud Messaging)
【Androidプッシュ通知】GCMの廃止、FCMへの移行とは? | 株式会社アイリッジ
https://iridge.jp/blog/201810/23018/
「Googleクラウドメッセージング(GCM)」が1年後に廃止、「Firebase Cloud Messaging(FCM)」への移行が必要に:Googleのアプリメッセージング基盤が完全に交代 - @IT
http://www.atmarkit.co.jp/ait/articles/1804/13/news051.html
【Androidプッシュ通知】GCMの廃止、FCMへの移行とは? | 株式会社アイリッジ
https://iridge.jp/blog/201810/23018/