ソースコード source code

下記アプリの主要なソースコードを公開しています。アプリ開発の参考になれば幸いです。

画像等が別途必要ですので下記情報のみでアプリが完成するものではありません。 アプリは少しずつ機能拡張していますのでストア公開されているアプリと内容が異なる場合があります。 コードはコピーして自由にお使いいただけます。ただし著作権は放棄しておりませんので全部の再掲載はご遠慮ください。部分的に再掲載したり、改変して再掲載するのは構いません。 自身のアプリ作成の参考として個人使用・商用問わず自由にお使いいただけます。 コード記述のお手本を示すものではありません。ミニアプリですので変数名などさほど気遣いしていない部分も有りますし間違いも有るかと思いますので参考程度にお考え下さい。 他の賢者の皆様が公開されているコードを参考にした箇所も含まれます。Androidアプリ開発の熟練者が書いたコードではありません。 エンジニア向け技術情報共有サービスではありませんので説明は省いています。ご了承ください。 GitHubなどへの公開は予定しておりません。

下記コードの最終ビルド日: 2021-05-08

build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.4.32"
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app/build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "jp.aosystem.dice"
        minSdkVersion 21
        targetSdkVersion 30
        multiDexEnabled true
        versionCode 7
        versionName "1.6"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.android.gms:play-services-ads:20.1.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

app/src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jp.aosystem.dice">

    <uses-permission android:name="android.permission.INTERNET" />

    <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/Theme.Dice">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SettingActivity" />

        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-0000000000000000~0000000000">
    </application>

</manifest>

app/src/main/java/jp/aosystem/dice/MainActivity.kt

package jp.aosystem.dice

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.os.*
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.constraintlayout.widget.ConstraintLayout
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.MobileAds
import jp.aosystem.dice.databinding.ActivityMainBinding
import java.util.*
import kotlin.random.Random


class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var viewModel: ConstraintLayout
    //
    private var baseWidth: Int = 0
    private var baseHeight: Int = 0
    private val resourceImg: Array<Int> = arrayOf(
        R.drawable.dice0,
        R.drawable.dice1,
        R.drawable.dice2,
        R.drawable.dice3,
        R.drawable.dice4,
        R.drawable.dice5,
        R.drawable.dice6,
        R.drawable.dice7,
        R.drawable.dice8,
        R.drawable.dice9,
        R.drawable.dice10,
        R.drawable.dice11,
        R.drawable.dice12,
    )
    private var diceImage: Array<ImageView> = arrayOf()
    private var actionCount: Int = 0    //回転の制御
    private var destroyFlag: Boolean = false    //Activity破棄された場合など
    private var diceCount: Int = 1
    private var themeNumber: Int = 0
    private var localeLanguage: String = ""
    //adMob
    private lateinit var adView: AdView     //adMob
    private val adSize: AdSize
        get() {
            val display = windowManager.defaultDisplay
            val outMetrics = DisplayMetrics()
            display.getMetrics(outMetrics)
            val density = outMetrics.density
            var adWidthPixels = this.binding.adContainer.width.toFloat()
            if (adWidthPixels == 0f) {
                adWidthPixels = outMetrics.widthPixels.toFloat()
            }
            val adWidth = (adWidthPixels / density).toInt()
            return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
        }

    companion object {
        //private const val AD_UNIT_ID: String = "ca-app-pub-3940256099942544/6300978111"     //adMob Test
        private const val AD_UNIT_ID: String = "ca-app-pub-0000000000000000/0000000000"     //adMob
        private const val RESULT_SETTING_ACTIVITY: Int = 1
        internal const val SETTINGS: String = "settings"
        internal const val DICE_COUNT: String = "diceCount"
        internal const val THEME_NUMBER: String = "themeNumber"
        internal const val LOCALE_LANGUAGE: String = "localeLanguage"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)
        this.binding = ActivityMainBinding.inflate(layoutInflater)
        this.viewModel = this.binding.root
        setContentView(this.viewModel)
        //タイトルバー非表示
        supportActionBar?.hide()
        //テーマ読み込みと設定
        this.loadThemeNumber()
        this.setTheme()
        //設定読み出し
        this.loadDiceCount()
        //adMob
        MobileAds.initialize(this) {}
        this.adView = AdView(this)
        this.binding.adContainer.addView(this.adView)
        this.loadBanner()
    }

    override fun onResume() {
        super.onResume()
        //準備が出来たらwidth,heightを取得して次へ進む
        this.binding.layoutBase.viewTreeObserver.addOnGlobalLayoutListener(object: OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                this@MainActivity.binding.layoutBase.viewTreeObserver.removeOnGlobalLayoutListener(this)
                this@MainActivity.baseWidth = this@MainActivity.binding.layoutBase.width
                this@MainActivity.baseHeight = this@MainActivity.binding.layoutBase.height
                this@MainActivity.initDice()
            }
        })
    }

    override fun onDestroy() {
        this.destroyFlag = true
        super.onDestroy()
    }

    //adMob
    private fun loadBanner() {
        this.adView.adUnitId = AD_UNIT_ID
        this.adView.adSize = adSize
        val adRequest = AdRequest
            .Builder()
            //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build()
        this.adView.loadAd(adRequest)
    }

    private fun initDice() {
        this.binding.layoutBase.removeAllViews()
        this.diceImage = arrayOf(
            ImageView(this),
            ImageView(this),
            ImageView(this),
            ImageView(this),
            ImageView(this),
            ImageView(this),
        )
        val boxSize: Int = minOf(this.baseWidth,this.baseHeight)
        when (this.diceCount) {
            1 -> {
                this.diceImage[0].setImageResource(resourceImg[12])
                val diceSize: Int = boxSize
                val marginTop: Int = (this.baseHeight - boxSize).div(2)
                val linearLayout: LinearLayout = LinearLayout(this)
                linearLayout.y = marginTop.toFloat()
                linearLayout.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                linearLayout.gravity = Gravity.CENTER
                linearLayout.addView(this.diceImage[0],diceSize,diceSize)
                this.binding.layoutBase.addView(linearLayout)
            }
            2 -> {
                this.diceImage[0].setImageResource(resourceImg[11])
                this.diceImage[1].setImageResource(resourceImg[12])
                var diceSize: Int = boxSize.div(2)
                var marginTop: Int = (this.baseHeight - boxSize.div(2)).div(2)
                if (this.baseWidth > this.baseHeight) {    //横
                    diceSize = minOf(this.baseWidth.div(2),this.baseHeight)
                    marginTop = (this.baseHeight - diceSize).div(2)
                }
                val linearLayout: LinearLayout = LinearLayout(this)
                linearLayout.y = marginTop.toFloat()
                linearLayout.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                linearLayout.gravity = Gravity.CENTER
                linearLayout.addView(this.diceImage[0],diceSize,diceSize)
                linearLayout.addView(this.diceImage[1],diceSize,diceSize)
                this.binding.layoutBase.addView(linearLayout)
            }
            3 -> {
                this.diceImage[0].setImageResource(resourceImg[10])
                this.diceImage[1].setImageResource(resourceImg[11])
                this.diceImage[2].setImageResource(resourceImg[12])
                if (this.baseWidth > this.baseHeight) {    //横
                    val diceSize: Int = minOf(this.baseWidth.div(3),this.baseHeight)
                    val marginTop: Int = (this.baseHeight - diceSize).div(2)
                    val linearLayout1: LinearLayout = LinearLayout(this)
                    linearLayout1.y = marginTop.toFloat()
                    linearLayout1.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout1.gravity = Gravity.CENTER
                    linearLayout1.addView(this.diceImage[0], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[1], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[2], diceSize, diceSize)
                    this.binding.layoutBase.addView(linearLayout1)
                } else {    //縦
                    val diceSize: Int = boxSize.div(2)
                    val marginTop: Int = (this.baseHeight - diceSize.times(2)).div(2)
                    val linearLayout1: LinearLayout = LinearLayout(this)
                    linearLayout1.y = marginTop.toFloat()
                    linearLayout1.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout1.gravity = Gravity.CENTER
                    linearLayout1.addView(this.diceImage[0],diceSize,diceSize)
                    linearLayout1.addView(this.diceImage[1],diceSize,diceSize)
                    this.binding.layoutBase.addView(linearLayout1)
                    val linearLayout2: LinearLayout = LinearLayout(this)
                    linearLayout2.y = marginTop.toFloat()
                    linearLayout2.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout2.gravity = Gravity.CENTER
                    linearLayout2.addView(this.diceImage[2],diceSize,diceSize)
                    this.binding.layoutBase.addView(linearLayout2)
                }
            }
            4 -> {
                this.diceImage[0].setImageResource(resourceImg[9])
                this.diceImage[1].setImageResource(resourceImg[10])
                this.diceImage[2].setImageResource(resourceImg[11])
                this.diceImage[3].setImageResource(resourceImg[12])
                if (this.baseWidth > this.baseHeight) {    //横
                    val diceSize: Int = minOf(this.baseWidth.div(4),this.baseHeight)
                    val marginTop: Int = (this.baseHeight - diceSize).div(2)
                    val linearLayout1: LinearLayout = LinearLayout(this)
                    linearLayout1.y = marginTop.toFloat()
                    linearLayout1.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout1.gravity = Gravity.CENTER
                    linearLayout1.addView(this.diceImage[0], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[1], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[2], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[3], diceSize, diceSize)
                    this.binding.layoutBase.addView(linearLayout1)
                } else {    //縦
                    val diceSize: Int = boxSize.div(2)
                    val marginTop: Int = (this.baseHeight - diceSize.times(2)).div(2)
                    val linearLayout1: LinearLayout = LinearLayout(this)
                    linearLayout1.y = marginTop.toFloat()
                    linearLayout1.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout1.gravity = Gravity.CENTER
                    linearLayout1.addView(this.diceImage[0],diceSize,diceSize)
                    linearLayout1.addView(this.diceImage[1],diceSize,diceSize)
                    this.binding.layoutBase.addView(linearLayout1)
                    val linearLayout2: LinearLayout = LinearLayout(this)
                    linearLayout2.y = marginTop.toFloat()
                    linearLayout2.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout2.gravity = Gravity.CENTER
                    linearLayout2.addView(this.diceImage[2],diceSize,diceSize)
                    linearLayout2.addView(this.diceImage[3],diceSize,diceSize)
                    this.binding.layoutBase.addView(linearLayout2)
                }
            }
            5 -> {
                this.diceImage[0].setImageResource(resourceImg[8])
                this.diceImage[1].setImageResource(resourceImg[9])
                this.diceImage[2].setImageResource(resourceImg[10])
                this.diceImage[3].setImageResource(resourceImg[11])
                this.diceImage[4].setImageResource(resourceImg[12])
                if (this.baseWidth > this.baseHeight) {    //横
                    val diceSize: Int = minOf(this.baseWidth.div(5),this.baseHeight)
                    val marginTop: Int = (this.baseHeight - diceSize).div(2)
                    val linearLayout1: LinearLayout = LinearLayout(this)
                    linearLayout1.y = marginTop.toFloat()
                    linearLayout1.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout1.gravity = Gravity.CENTER
                    linearLayout1.addView(this.diceImage[0], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[1], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[2], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[3], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[4], diceSize, diceSize)
                    this.binding.layoutBase.addView(linearLayout1)
                } else {    //縦
                    val diceSize: Int = boxSize.div(3)
                    val marginTop: Int = (this.baseHeight - diceSize.times(3)).div(2)
                    val linearLayout1: LinearLayout = LinearLayout(this)
                    linearLayout1.y = marginTop.toFloat()
                    linearLayout1.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout1.gravity = Gravity.CENTER
                    linearLayout1.addView(this.diceImage[0],diceSize,diceSize)
                    linearLayout1.addView(this.diceImage[1],diceSize,diceSize)
                    this.binding.layoutBase.addView(linearLayout1)
                    val linearLayout2: LinearLayout = LinearLayout(this)
                    linearLayout2.y = marginTop.toFloat()
                    linearLayout2.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout2.gravity = Gravity.CENTER
                    linearLayout2.addView(this.diceImage[2],diceSize,diceSize)
                    linearLayout2.addView(this.diceImage[3],diceSize,diceSize)
                    this.binding.layoutBase.addView(linearLayout2)
                    val linearLayout3: LinearLayout = LinearLayout(this)
                    linearLayout3.y = marginTop.toFloat()
                    linearLayout3.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout3.gravity = Gravity.CENTER
                    linearLayout3.addView(this.diceImage[4],diceSize,diceSize)
                    this.binding.layoutBase.addView(linearLayout3)
                }
            }
            6 -> {
                this.diceImage[0].setImageResource(resourceImg[7])
                this.diceImage[1].setImageResource(resourceImg[8])
                this.diceImage[2].setImageResource(resourceImg[9])
                this.diceImage[3].setImageResource(resourceImg[10])
                this.diceImage[4].setImageResource(resourceImg[11])
                this.diceImage[5].setImageResource(resourceImg[12])
                if (this.baseWidth > this.baseHeight) {    //横
                    val diceSize: Int = minOf(this.baseWidth.div(6),this.baseHeight)
                    val marginTop: Int = (this.baseHeight - diceSize).div(2)
                    val linearLayout1: LinearLayout = LinearLayout(this)
                    linearLayout1.y = marginTop.toFloat()
                    linearLayout1.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout1.gravity = Gravity.CENTER
                    linearLayout1.addView(this.diceImage[0], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[1], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[2], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[3], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[4], diceSize, diceSize)
                    linearLayout1.addView(this.diceImage[5], diceSize, diceSize)
                    this.binding.layoutBase.addView(linearLayout1)
                } else {    //縦
                    val diceSize: Int = boxSize.div(3)
                    val marginTop: Int = (this.baseHeight - diceSize.times(3)).div(2)
                    val linearLayout1: LinearLayout = LinearLayout(this)
                    linearLayout1.y = marginTop.toFloat()
                    linearLayout1.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout1.gravity = Gravity.CENTER
                    linearLayout1.addView(this.diceImage[0],diceSize,diceSize)
                    linearLayout1.addView(this.diceImage[1],diceSize,diceSize)
                    this.binding.layoutBase.addView(linearLayout1)
                    val linearLayout2: LinearLayout = LinearLayout(this)
                    linearLayout2.y = marginTop.toFloat()
                    linearLayout2.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout2.gravity = Gravity.CENTER
                    linearLayout2.addView(this.diceImage[2],diceSize,diceSize)
                    linearLayout2.addView(this.diceImage[3],diceSize,diceSize)
                    this.binding.layoutBase.addView(linearLayout2)
                    val linearLayout3: LinearLayout = LinearLayout(this)
                    linearLayout3.y = marginTop.toFloat()
                    linearLayout3.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    linearLayout3.gravity = Gravity.CENTER
                    linearLayout3.addView(this.diceImage[4],diceSize,diceSize)
                    linearLayout3.addView(this.diceImage[5],diceSize,diceSize)
                    this.binding.layoutBase.addView(linearLayout3)
                }
            }
        }
    }

    fun onClickStart(v: View) {
        this.destroyFlag = false
        this.actionCount = 0
        this.actionTimeline()
    }

    private fun actionTimeline() {
        Handler(Looper.getMainLooper()).postDelayed({
            if (this.destroyFlag) {
                return@postDelayed
            }
            actionStart(false)
            this.actionCount += 1
            if (this.actionCount > 50) {
                this.actionCount = 0
                if (this.destroyFlag) {
                    return@postDelayed
                }
                actionStart(true)
                return@postDelayed
            }
            actionTimeline()
        }, 20)
    }

    private fun actionStart(endFlag: Boolean) {
        if (endFlag) {
            if (this.diceCount >= 1) {
                this.diceImage[0].setImageResource(resourceImg[Random.nextInt(6) + 1])
            }
            if (this.diceCount >= 2) {
                this.diceImage[1].setImageResource(resourceImg[Random.nextInt(6) + 1])
            }
            if (this.diceCount >= 3) {
                this.diceImage[2].setImageResource(resourceImg[Random.nextInt(6) + 1])
            }
            if (this.diceCount >= 4) {
                this.diceImage[3].setImageResource(resourceImg[Random.nextInt(6) + 1])
            }
            if (this.diceCount >= 5) {
                this.diceImage[4].setImageResource(resourceImg[Random.nextInt(6) + 1])
            }
            if (this.diceCount >= 6) {
                this.diceImage[5].setImageResource(resourceImg[Random.nextInt(6) + 1])
            }
        } else {
            if (this.diceCount >= 1) {
                this.diceImage[0].setImageResource(resourceImg[Random.nextInt(6) + 7])
            }
            if (this.diceCount >= 2) {
                this.diceImage[1].setImageResource(resourceImg[Random.nextInt(6) + 7])
            }
            if (this.diceCount >= 3) {
                this.diceImage[2].setImageResource(resourceImg[Random.nextInt(6) + 7])
            }
            if (this.diceCount >= 4) {
                this.diceImage[3].setImageResource(resourceImg[Random.nextInt(6) + 7])
            }
            if (this.diceCount >= 5) {
                this.diceImage[4].setImageResource(resourceImg[Random.nextInt(6) + 7])
            }
            if (this.diceCount >= 6) {
                this.diceImage[5].setImageResource(resourceImg[Random.nextInt(6) + 7])
            }
        }
    }

    fun onClickSetting(v: View) {
        this.destroyFlag = true
        val intent = Intent(applicationContext, SettingActivity::class.java)
        intent.putExtra(DICE_COUNT, this.diceCount)
        intent.putExtra(THEME_NUMBER, this.themeNumber)
        intent.putExtra(LOCALE_LANGUAGE, this.localeLanguage)
        startActivityForResult(intent, RESULT_SETTING_ACTIVITY)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
        super.onActivityResult(requestCode, resultCode, intent)
        if (resultCode == Activity.RESULT_OK && requestCode == RESULT_SETTING_ACTIVITY && intent != null) {
            var recreateFlag: Boolean = false
            val lastDiceCount = this.diceCount
            this.diceCount = intent.getIntExtra(DICE_COUNT, 1)
            if (lastDiceCount != this.diceCount) {
                this.saveDiceCount()
                recreateFlag = true
            }
            val lastThemeNumber = this.themeNumber
            this.themeNumber = intent.getIntExtra(THEME_NUMBER, 0)
            if (lastThemeNumber != this.themeNumber) {
                this.saveThemeNumber()
                recreateFlag = true
            }
            val lastLocaleLanguage = this.localeLanguage
            this.localeLanguage = intent.getStringExtra(LOCALE_LANGUAGE) ?: ""
            if (this.localeLanguage != lastLocaleLanguage) {
                this.saveLocaleLanguage()
                recreateFlag = true
            }
            if (recreateFlag) {
                recreate()
            }
        }
    }

    //-------------------------------------------

    //diceCountを保存
    private fun saveDiceCount() {
        getSharedPreferences(SETTINGS, Context.MODE_PRIVATE).edit().apply {
            putInt(DICE_COUNT, this@MainActivity.diceCount)
            apply()
        }
    }

    //diceCountを読み出し
    private fun loadDiceCount() {
        val pref = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
        this.diceCount = pref.getInt(DICE_COUNT, 1)
    }

    //-------------------------------------------

    //テーマを保存
    private fun saveThemeNumber() {
        getSharedPreferences(SETTINGS, Context.MODE_PRIVATE).edit().apply {
            putInt(THEME_NUMBER, this@MainActivity.themeNumber)
            apply()
        }
    }

    //テーマを読み出し
    private fun loadThemeNumber() {
        val pref = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
        this.themeNumber = pref.getInt(THEME_NUMBER, 0)
    }

    //テーマを設定
    private fun setTheme() {
        when (this.themeNumber) {
            0 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            1 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
        }
    }

    //-------------------------------------------

    //localeLanguageを保存
    private fun saveLocaleLanguage() {
        getSharedPreferences(SETTINGS, Context.MODE_PRIVATE).edit().apply {
            putString(LOCALE_LANGUAGE, this@MainActivity.localeLanguage)
            apply()
        }
    }

    //言語設定
    override fun attachBaseContext(base: Context) {
        val pref = base.getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
        this.localeLanguage = pref.getString(LOCALE_LANGUAGE, "") ?: ""
        val loc: Locale? = if (this.localeLanguage != "") Locale(this.localeLanguage) else null
        if (loc != null) {
            val res = base.resources
            val config = Configuration(res.configuration)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {    //minSdkVersion 24
                val localeList = LocaleList(loc)
                LocaleList.setDefault(localeList)
                config.setLocales(localeList)
            } else {    //minSdkVersion 17  16はダメ
                config.setLocale(loc)
            }
            super.attachBaseContext(base.createConfigurationContext(config))
        } else {
            super.attachBaseContext(base)
        }
    }

    //----------------------------------------------

}

app/src/main/java/jp/aosystem/dice/SettingActivity.kt

package jp.aosystem.dice

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.os.LocaleList
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import jp.aosystem.dice.databinding.ActivitySettingBinding
import java.util.*

class SettingActivity : AppCompatActivity() {
    private lateinit var binding: ActivitySettingBinding
    private lateinit var viewModel: ConstraintLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)
        this.binding = ActivitySettingBinding.inflate(layoutInflater)
        this.viewModel = this.binding.root
        setContentView(this.viewModel)
        //データ受け取り
        val intent = this.intent
        val diceCount: Int = intent.getIntExtra(MainActivity.DICE_COUNT,1)
        val themeNumber: Int = intent.getIntExtra(MainActivity.THEME_NUMBER,0)
        when (diceCount) {
            1 -> this.binding.radioDiceCount1.isChecked = true
            2 -> this.binding.radioDiceCount2.isChecked = true
            3 -> this.binding.radioDiceCount3.isChecked = true
            4 -> this.binding.radioDiceCount4.isChecked = true
            5 -> this.binding.radioDiceCount5.isChecked = true
            6 -> this.binding.radioDiceCount6.isChecked = true
        }
        this.binding.switchTheme.isChecked = themeNumber != 0
        val localeLanguage: String = intent.getStringExtra(MainActivity.LOCALE_LANGUAGE) ?: ""
        when (localeLanguage) {
            "en" -> this.binding.radioLanguageEn.isChecked = true
            "ja" -> this.binding.radioLanguageJa.isChecked = true
            else -> this.binding.radioLanguageSystem.isChecked = true
        }
        //タイトルバー非表示
        supportActionBar?.hide()
    }

    fun onClickApply(v: View) {
        var diceCount: Int = 1
        if (this.binding.radioDiceCount1.isChecked) {
            diceCount = 1
        } else if (this.binding.radioDiceCount2.isChecked) {
            diceCount = 2
        } else if (this.binding.radioDiceCount3.isChecked) {
            diceCount = 3
        } else if (this.binding.radioDiceCount4.isChecked) {
            diceCount = 4
        } else if (this.binding.radioDiceCount5.isChecked) {
            diceCount = 5
        } else if (this.binding.radioDiceCount6.isChecked) {
            diceCount = 6
        }
        val themeNumber = if (this.binding.switchTheme.isChecked) 1 else 0
        var localeLanguage: String = ""
        if (this.binding.radioLanguageEn.isChecked) {
            localeLanguage = "en"
        } else if (this.binding.radioLanguageJa.isChecked) {
            localeLanguage = "ja"
        }
        val intent = Intent()
        intent.putExtra(MainActivity.DICE_COUNT, diceCount)
        intent.putExtra(MainActivity.THEME_NUMBER, themeNumber)
        intent.putExtra(MainActivity.LOCALE_LANGUAGE, localeLanguage)
        setResult(Activity.RESULT_OK, intent)
        finish()
    }

    fun onClickCancel(v: View) {
        val intent = Intent()
        setResult(Activity.RESULT_CANCELED, intent)
        finish()
    }

    //--------------------------------------------------------------------------------

    //言語設定
    override fun attachBaseContext(base: Context) {
        val pref = base.getSharedPreferences(MainActivity.SETTINGS, Context.MODE_PRIVATE)
        val localeLanguage: String = pref.getString(MainActivity.LOCALE_LANGUAGE, "") ?: ""
        val loc: Locale? = if (localeLanguage != "") Locale(localeLanguage) else null
        if (loc != null) {
            val res = base.resources
            val config = Configuration(res.configuration)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {    //minSdkVersion 24
                val localeList = LocaleList(loc)
                LocaleList.setDefault(localeList)
                config.setLocales(localeList)
            } else {    //minSdkVersion 17  16はダメ
                config.setLocale(loc)
            }
            super.attachBaseContext(base.createConfigurationContext(config))
        } else {
            super.attachBaseContext(base)
        }
    }

    //--------------------------------------------------------------------------------

}

app/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">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#007c40"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/layoutHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textStart"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="7"
                android:background="#7462C5"
                android:gravity="center"
                android:minHeight="96dip"
                android:onClick="onClickStart"
                android:text="@string/shakeTheDice" />

            <TextView
                android:id="@+id/textSetting"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="1dp"
                android:layout_weight="1"
                android:background="#6276C5"
                android:gravity="center"
                android:minHeight="96dip"
                android:onClick="onClickSetting"
                android:text="@string/setting" />

        </LinearLayout>

        <Space
            android:layout_width="match_parent"
            android:layout_height="10dp" />

        <LinearLayout
            android:id="@+id/layoutBase"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical">

        </LinearLayout>

        <Space
            android:layout_width="match_parent"
            android:layout_height="100dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ad_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent" />



</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/layout/activity_setting.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=".SettingActivity">

    <LinearLayout
        android:id="@+id/layoutButtons"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        android:layout_marginTop="1dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#86C663"
            android:gravity="center"
            android:minHeight="48dp"
            android:onClick="onClickCancel"
            android:text="@string/cancel" />

        <TextView
            android:id="@+id/textVerification"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="1dp"
            android:layout_weight="1"
            android:background="#5DC674"
            android:gravity="center"
            android:minHeight="48dp"
            android:onClick="onClickApply"
            android:text="@string/apply" />

    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/layoutButtons">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="20dp"
            android:paddingTop="20dp"
            android:paddingRight="20dp"
            android:paddingBottom="50dp">

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:text="@string/numberOfDice"
                android:textColor="?android:attr/textColorPrimary" />

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/radioDiceCount1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/one" />

                <RadioButton
                    android:id="@+id/radioDiceCount2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/two" />

                <RadioButton
                    android:id="@+id/radioDiceCount3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/three" />

                <RadioButton
                    android:id="@+id/radioDiceCount4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/four" />

                <RadioButton
                    android:id="@+id/radioDiceCount5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/five" />

                <RadioButton
                    android:id="@+id/radioDiceCount6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/six" />

            </RadioGroup>

            <View
                android:id="@+id/divider1"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="20dp"
                android:background="?android:attr/listDivider" />

            <androidx.appcompat.widget.SwitchCompat
                android:id="@+id/switchTheme"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:text="@string/darkTheme" />

            <View
                android:id="@+id/divider3"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="10dp"
                android:background="?android:attr/listDivider" />

            <TextView
                android:id="@+id/textLanguage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="@string/language"
                android:textColor="?android:attr/textColorPrimary" />

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
                <RadioButton
                    android:id="@+id/radioLanguageSystem"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageSystem" />
                <RadioButton
                    android:id="@+id/radioLanguageEn"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageEn" />
                <RadioButton
                    android:id="@+id/radioLanguageJa"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageJa" />

            </RadioGroup>

            <View
                android:id="@+id/divider2"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="20dp"
                android:background="?android:attr/listDivider" />

        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>