ソースコード 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.eyedropsfromupstairs"
        minSdkVersion 21
        targetSdkVersion 30
        multiDexEnabled true
        versionCode 9
        versionName "1.8"
        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.eyedropsfromupstairs">

    <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.EyeDropsFromUpstairs">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <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/eyedropsfromupstairs/MainActivity.kt

package jp.aosystem.eyedropsfromupstairs

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.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 androidx.core.content.ContextCompat
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.eyedropsfromupstairs.databinding.ActivityMainBinding
import java.util.*
import kotlin.random.Random


class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var viewModel: ConstraintLayout
    //
    private val levels: List<Int> = listOf(
            500,      //level0
            250,     //level1
            200,    //level2
            180,    //level3
            160,    //level4
            140,    //level5
            120,    //level6
            100,    //level7
            80,    //level8
            60,    //level9
            40,   //level10
    )
    private val manXs: List<Float> = listOf(  //man woman の横位置
            50F,
            143F,
            236F,
            329F,
            421F,
            513F,
            605.5F,
            698F,
    )
    private val dropYs: List<Float> = listOf(  //dropの縦位置
            220F,
            230F,
            240F,
            250F,
            260F,
            270F,
            280F,
            290F,
            300F,
            310F,
            320F,
            330F,
            340F,
            350F,
            360F,
            370F,
            380F,
            390F,
            400F,
            410F,
            420F,
            430F,
            440F,
            450F,
            460F,
            470F,
            480F,
            490F,
            500F,
            510F,
            520F,
            530F,
            540F,
            550F,
            560F,
            570F,   //眼の所
            610F,
            650F,
    )
    data class DropData (   //目薬
            var enable: Boolean,    //有効か否か
            var x: Int,     //窓の位置0~7
            var y: Int,     //上下の位置0~9
            var layout: LinearLayout,   //Layout
    )
    private val dropCount: Int = 20 //目薬の数
    private var drops: Array<DropData> = arrayOf()  //dropのデータ
    private val manY: Float = 54F
    private val womanY: Float = 554F
    data class ResourceImg (
            var img: Int,
            var w: Int,
            var h: Int,
    )
    private val resourceHouse: ResourceImg = ResourceImg(R.drawable.ic_house, 900 ,900)
    private val resourceMan: ResourceImg = ResourceImg(R.drawable.ic_man, 117, 179)
    private val resourceWoman: ResourceImg = ResourceImg(R.drawable.ic_woman, 186, 333)
    private val resourceDrop: ResourceImg = ResourceImg(R.drawable.ic_drop,  20, 18)
    private lateinit var womanLayout: LinearLayout
    private lateinit var manLayout: LinearLayout
    private var baseWidth: Int = 0      //描画エリア
    private var baseHeight: Int = 0     //描画エリア
    private var viewRatio: Float = 0F   //画像縮小率
    private var marginLeft: Float = 0F  //左側マージン
    private var destroyFlag: Boolean = false    //Activity破棄された場合などはtrue
    private var busyFlag: Boolean = false    //PLAY中はtrue
    private var difficultyLevel: Int = 1       //難易度
    private var themeNumber: Int = 0    //テーマの種類
    private var localeLanguage: String = ""
    private var positionMan: Int = 2    //初期立ち位置
    private var positionWoman: Int = 7  //初期立ち位置
    private var timelineCount: Int = 0  //タイムラインのループでカウントアップしていく
    private var score: Int = 0      //ゲームのSCORE
    private var lastResultBackColor: Boolean = false

    //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 DIFFICULTY_LEVEL: String = "difficultyLevel"
        internal const val SETTINGS: String = "settings"
        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.loadDifficultyLevel()
        this.loadThemeNumber()
        this.setTheme()
        //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.init()
            }
        })
    }

    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 init() {
        this.binding.layoutBase.removeAllViews()
        if (this.baseWidth.div(this.baseHeight) < this.resourceHouse.w.div(this.resourceHouse.h)) { //横がぴったり
            this.viewRatio = this.baseWidth.div(this.resourceHouse.w.toFloat())
            this.marginLeft = 0F
        } else {    //横に隙間
            this.viewRatio = this.baseHeight.div(this.resourceHouse.h.toFloat())
            this.marginLeft = (this.baseWidth - this.resourceHouse.w.toFloat().times(this.viewRatio)).div(2)
        }
        //家
        val houseLayout: LinearLayout = LinearLayout(this)
        houseLayout.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        houseLayout.x = this.marginLeft
        houseLayout.y = 0F
        val houseWidth: Float = this.resourceHouse.w.toFloat().times(this.viewRatio)
        val houseHeight: Float = this.resourceHouse.h.toFloat().times(this.viewRatio)
        val imgHouse: ImageView = ImageView(this)
        imgHouse.setImageResource(this.resourceHouse.img)
        houseLayout.addView(imgHouse, houseWidth.toInt(), houseHeight.toInt())
        this.binding.layoutBase.addView(houseLayout)
        //woman
        this.womanLayout = LinearLayout(this)
        this.womanLayout.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        val womanWidth: Float = this.resourceWoman.w.toFloat().times(this.viewRatio)
        val womanHeight: Float = this.resourceWoman.h.toFloat().times(this.viewRatio)
        val imgWoman: ImageView = ImageView(this)
        imgWoman.setImageResource(this.resourceWoman.img)
        this.womanLayout.addView(imgWoman, womanWidth.toInt(), womanHeight.toInt())
        this.binding.layoutBase.addView(this.womanLayout)
        //man
        this.manLayout = LinearLayout(this)
        this.manLayout.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        val manWidth: Float = this.resourceMan.w.toFloat().times(this.viewRatio)
        val manHeight: Float = this.resourceMan.h.toFloat().times(this.viewRatio)
        val imgMan: ImageView = ImageView(this)
        imgMan.setImageResource(this.resourceMan.img)
        this.manLayout.addView(imgMan, manWidth.toInt(), manHeight.toInt())
        this.binding.layoutBase.addView(this.manLayout)
        //man womanの位置
        this.manLayout.y = this.manY.times(this.viewRatio)
        this.womanLayout.y = this.womanY.times(this.viewRatio)
        manPosition()
        womanPosition()
        //drop
        this.drops = arrayOf()
        for (i in 0..this.dropCount) {
            val lay = LinearLayout(this)
            lay.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            val dropWidth: Float = this.resourceDrop.w.toFloat().times(this.viewRatio)
            val dropHeight: Float = this.resourceDrop.h.toFloat().times(this.viewRatio)
            val imgDrop: ImageView = ImageView(this)
            imgDrop.setImageResource(this.resourceDrop.img)
            lay.addView(imgDrop, dropWidth.toInt(), dropHeight.toInt())
            lay.y = 0F
            lay.y = 0F
            lay.alpha = 0F
            this.binding.layoutBase.addView(lay)
            val drop = DropData(false,0,0,lay)
            this.drops += drop
        }
        //result表示
        this.binding.textResult.text = "LEVEL: " + this.difficultyLevel.toString()
    }

    private fun manPosition() {
        this.manLayout.x = this.manXs[this.positionMan].times(this.viewRatio) + this.marginLeft
    }

    private fun womanPosition() {
        val adjust: Float = 0 - 20.times(this.viewRatio)
        this.womanLayout.x = this.manXs[this.positionWoman].times(this.viewRatio) + this.marginLeft + adjust
    }

    private fun dropPosition(num: Int) {
        val adjust: Float = 62.times(this.viewRatio)
        this.drops[num].layout.x = this.manXs[this.drops[num].x].times(this.viewRatio) + this.marginLeft + adjust
        this.drops[num].layout.y = this.dropYs[this.drops[num].y].times(this.viewRatio)
    }

    fun onClickWomanLeft(v: View) {
        if (this.positionWoman > 0) {
            this.positionWoman -= 1
            womanPosition()
        }
    }

    fun onClickWomanRight(v: View) {
        if (this.positionWoman < (this.manXs.size - 1)) {
            this.positionWoman += 1
            womanPosition()
        }
    }

    fun onClickStart(v: View) {
        if (this.busyFlag) {
            return
        }
        this.busyFlag = true
        this.binding.textStart.alpha = 0F
        this.binding.textSetting.alpha = 0F
        this.init()
        this.destroyFlag = false
        this.timelineCount = 0
        this.score = 0
        this.resultBackColor(true)
        this.resultBackColor(false)
        this.actionTimeline()
    }

    private fun resultText(tick: Int) {
        val progressLeft: String = if (this.busyFlag) ".".repeat(10 - tick) + "|".repeat(tick) else ""
        val progressRight: String = if (this.busyFlag) "|".repeat(tick) + ".".repeat(10 - tick) else ""
        this.binding.textResult.text = progressLeft + " LEVEL: " + this.difficultyLevel.toString() + " SCORE: " + this.score + " " + progressRight
    }

    private fun resultBackColor(onOff: Boolean) {
        if (this.lastResultBackColor == onOff) {
            return
        }
        this.lastResultBackColor = onOff
        val c: Int = when (this.busyFlag) {
            true -> if (this.lastResultBackColor) ContextCompat.getColor(this, R.color.resultHit) else ContextCompat.getColor(this, R.color.resultNormal)
            false -> ContextCompat.getColor(this, R.color.resultEnd)
        }
        this.binding.textResult.setBackgroundColor(c)
    }

    private fun actionTimeline() {
        timelineCount += 1
        Handler(Looper.getMainLooper()).postDelayed({
            if (this.destroyFlag) {
                return@postDelayed
            }
            this.dropStep()
            if (timelineCount % 10 == 0) {
                this.manStep()
                this.dropAdd()
            }
            this.resultText(timelineCount % 10)
            actionTimeline()
        }, this.levels[difficultyLevel].toLong())
    }

    private fun manStep() {
        this.positionMan = Random.nextInt(this.manXs.size)
        this.manPosition()
    }

    private fun dropStep() {
        this.resultBackColor(false)
        for (i in 0..dropCount) {
            if (this.drops[i].enable) {
                this.drops[i].y += 1
                this.dropPosition(i)
                if (this.drops[i].y == this.dropYs.size - 2) {
                    if (this.drops[i].x == this.positionWoman) {  //womanが下に居たら消す
                        this.drops[i].enable = false
                        this.drops[i].layout.alpha = 0F
                        this.score += 1
                        this.resultBackColor(true)
                    }
                }
                if (this.drops[i].y > this.dropYs.size - 2) {  //アウト
                    gameOver()
                    break
                }
            }
        }
    }

    private fun dropAdd() {
        for (i in 0..dropCount) {
            if (this.drops[i].enable == false) {
                this.drops[i].x = this.positionMan
                this.drops[i].y = 0
                this.drops[i].enable = true
                this.dropPosition(i)
                this.drops[i].layout.alpha = 1F
                break
            }
        }
    }

    private fun gameOver() {
        this.destroyFlag = true
        this.busyFlag = false
        this.binding.textStart.alpha = 1F
        this.binding.textSetting.alpha = 1F
        this.resultBackColor(true)
    }

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

    fun onClickSetting(v: View) {
        if (this.busyFlag) {
            return
        }
        this.destroyFlag = true
        val intent = Intent(applicationContext, SettingActivity::class.java)
        intent.putExtra(DIFFICULTY_LEVEL, this.difficultyLevel)
        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) {
            val lastDifficultyLevel = this.difficultyLevel
            this.difficultyLevel = intent.getIntExtra(DIFFICULTY_LEVEL, 1)
            if (lastDifficultyLevel != this.difficultyLevel) {
                this.saveDifficultyLevel()
            }
            val lastThemeNumber = this.themeNumber
            this.themeNumber = intent.getIntExtra(THEME_NUMBER, 0)
            if (lastThemeNumber != this.themeNumber) {
                this.saveThemeNumber()
            }
            val lastLocaleLanguage = this.localeLanguage
            this.localeLanguage = intent.getStringExtra(LOCALE_LANGUAGE) ?: ""
            if (this.localeLanguage != lastLocaleLanguage) {
                this.saveLocaleLanguage()
            }
        }
        recreate()
    }

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

    //difficultyLevelを保存
    private fun saveDifficultyLevel() {
        getSharedPreferences(SETTINGS, Context.MODE_PRIVATE).edit().apply {
            putInt(DIFFICULTY_LEVEL, this@MainActivity.difficultyLevel)
            apply()
        }
    }

    //difficultyLevelを読み出し
    private fun loadDifficultyLevel() {
        val pref = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
        this.difficultyLevel = pref.getInt(DIFFICULTY_LEVEL, 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/eyedropsfromupstairs/SettingActivity.kt

package jp.aosystem.eyedropsfromupstairs

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.eyedropsfromupstairs.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 difficultyLevel: Int = intent.getIntExtra(MainActivity.DIFFICULTY_LEVEL,1)
        val themeNumber: Int = intent.getIntExtra(MainActivity.THEME_NUMBER,0)
        when (difficultyLevel) {
            0 -> this.binding.radioLevel0.isChecked = true
            1 -> this.binding.radioLevel1.isChecked = true
            2 -> this.binding.radioLevel2.isChecked = true
            3 -> this.binding.radioLevel3.isChecked = true
            4 -> this.binding.radioLevel4.isChecked = true
            5 -> this.binding.radioLevel5.isChecked = true
            6 -> this.binding.radioLevel6.isChecked = true
            7 -> this.binding.radioLevel7.isChecked = true
            8 -> this.binding.radioLevel8.isChecked = true
            9 -> this.binding.radioLevel9.isChecked = true
            10 -> this.binding.radioLevel10.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 difficultyLevel: Int = 1
        if (this.binding.radioLevel0.isChecked) {
            difficultyLevel = 0
        } else if (this.binding.radioLevel1.isChecked) {
            difficultyLevel = 1
        } else if (this.binding.radioLevel2.isChecked) {
            difficultyLevel = 2
        } else if (this.binding.radioLevel3.isChecked) {
            difficultyLevel = 3
        } else if (this.binding.radioLevel4.isChecked) {
            difficultyLevel = 4
        } else if (this.binding.radioLevel5.isChecked) {
            difficultyLevel = 5
        } else if (this.binding.radioLevel6.isChecked) {
            difficultyLevel = 6
        } else if (this.binding.radioLevel7.isChecked) {
            difficultyLevel = 7
        } else if (this.binding.radioLevel8.isChecked) {
            difficultyLevel = 8
        } else if (this.binding.radioLevel9.isChecked) {
            difficultyLevel = 9
        } else if (this.binding.radioLevel10.isChecked) {
            difficultyLevel = 10
        }
        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"
        }
        intent.putExtra(MainActivity.DIFFICULTY_LEVEL, difficultyLevel)
        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:id="@+id/layoutBackground"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        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:background="#7CB342"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:gravity="center"
                android:minHeight="48dip"
                android:text="@string/needleHaystack" />

            <TextView
                android:id="@+id/textStart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#C0CA33"
                android:gravity="center"
                android:minHeight="48dip"
                android:onClick="onClickStart"
                android:text="@string/start" />

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

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textResult"
                android:layout_width="match_parent"
                android:layout_height="36dp"
                android:background="#81C784"
                android:gravity="center" />
        </LinearLayout>

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

        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textLeft"
                android:layout_width="wrap_content"
                android:layout_height="48dp"
                android:layout_weight="1"
                android:background="#64B5F6"
                android:gravity="center"
                android:onClick="onClickWomanLeft"
                android:text="@string/left" />

            <TextView
                android:id="@+id/textRight"
                android:layout_width="wrap_content"
                android:layout_height="48dp"
                android:layout_weight="1"
                android:background="#FF8A65"
                android:gravity="center"
                android:onClick="onClickWomanRight"
                android:text="@string/right" />
        </LinearLayout>

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




    </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"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="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: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="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:background="#FDD835"
            android:gravity="center"
            android:minHeight="48dip"
            android:onClick="onClickCancel"
            android:text="@string/cancel" />

        <TextView
            android:id="@+id/textVerification"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:background="#C0CA33"
            android:gravity="center"
            android:minHeight="48dip"
            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:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:orientation="vertical"
            android:paddingLeft="20dp"
            android:paddingTop="20dp"
            android:paddingRight="20dp"
            android:paddingBottom="50dp">


            <TextView
                android:id="@+id/textLevel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/level"
                android:textColor="?android:attr/textColorPrimary" />

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="10dp">

                <RadioButton
                    android:id="@+id/radioLevel0"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="18dp"
                    android:text="@string/level0" />

                <RadioButton
                    android:id="@+id/radioLevel1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="18dp"
                    android:text="@string/level1" />

                <RadioButton
                    android:id="@+id/radioLevel2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="18dp"
                    android:text="@string/level2" />

                <RadioButton
                    android:id="@+id/radioLevel3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="18dp"
                    android:text="@string/level3" />

                <RadioButton
                    android:id="@+id/radioLevel4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="18dp"
                    android:text="@string/level4" />

                <RadioButton
                    android:id="@+id/radioLevel5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="18dp"
                    android:text="@string/level5" />

                <RadioButton
                    android:id="@+id/radioLevel6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="18dp"
                    android:text="@string/level6" />

                <RadioButton
                    android:id="@+id/radioLevel7"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="18dp"
                    android:text="@string/level7" />

                <RadioButton
                    android:id="@+id/radioLevel8"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="18dp"
                    android:text="@string/level8" />

                <RadioButton
                    android:id="@+id/radioLevel9"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="18dp"
                    android:text="@string/level9" />

                <RadioButton
                    android:id="@+id/radioLevel10"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="18dp"
                    android:text="@string/level10" />
            </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="10dp"
                android:text="@string/darkTheme" />

            <View
                android:id="@+id/divider4"
                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="10dp"
                android:background="?android:attr/listDivider" />

            <TextView
                android:id="@+id/textDescription"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="@string/description" />

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

            <TextView
                android:id="@+id/textUsage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="@string/usage" />

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                app:srcCompat="@drawable/ic_usage1" />

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

        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>