フリーランス 技術調査ブログ

フリーランス/エンジニア Ruby Python Nodejs Vuejs React Dockerなどの調査技術調査の備忘録

android/kotlinでボタンクリック時にgmailを起動

res / layout /activity_main.xml

  • ボタンを設置して、ボタンクリック時にsendHtmlEmailメソッドが実行されるように設定する
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendHtmlEmail"
        android:text="Send HTML Email" />

</RelativeLayout>

src/MainActivity.kt

  • 下記のメソッド内でメールに設定したい内容を指定する
    fun sendHtmlEmail(view: View?) {
        val mailId = "<送信したいメールアドレス>@gmail.com"
        val emailIntent = Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", mailId, null))
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "件名が入ります。")
        emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<p><b>テスト</b></p>" +
                "<small><p>テスト</p></small>"))
        startActivity(Intent.createChooser(emailIntent, "メール送信・・・"))
    }

※上記の設定だけで完了。