IEには対応していません。Edgeでサイトを開く ≫

ソースコード 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.twosidesofcoin"
        minSdkVersion 21
        targetSdkVersion 30
        multiDexEnabled true
        versionCode 5
        versionName "1.4"
        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.twosidesofcoin">

    <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.TwoSidesOfCoin">
        <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/twosidesofcoin/MainActivity.kt

package jp.aosystem.twosidesofcoin

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.ViewTreeObserver.OnGlobalLayoutListener
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.twosidesofcoin.databinding.ActivityMainBinding
import java.util.*
import kotlin.random.Random


class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var viewModel: ConstraintLayout
    //
    private val resourceImgA: List<Int> = listOf(
        R.drawable.coin_a_001,
        R.drawable.coin_a_002,
        R.drawable.coin_a_003,
        R.drawable.coin_a_004,
        R.drawable.coin_a_005,
        R.drawable.coin_a_006,
        R.drawable.coin_a_007,
        R.drawable.coin_a_008,
        R.drawable.coin_a_009,
        R.drawable.coin_a_010,
        R.drawable.coin_a_011,
        R.drawable.coin_a_012,
        R.drawable.coin_a_013,
        R.drawable.coin_a_014,
        R.drawable.coin_a_015,
        R.drawable.coin_a_016,
        R.drawable.coin_a_017,
        R.drawable.coin_a_018,
        R.drawable.coin_a_019,
        R.drawable.coin_a_020,
        R.drawable.coin_a_021,
        R.drawable.coin_a_022,
        R.drawable.coin_a_023,
        R.drawable.coin_a_024,
        R.drawable.coin_a_025,
        R.drawable.coin_a_026,
        R.drawable.coin_a_027,
        R.drawable.coin_a_028,
        R.drawable.coin_a_029,
        R.drawable.coin_a_030,
        R.drawable.coin_a_031,
        R.drawable.coin_a_032,
        R.drawable.coin_a_033,
        R.drawable.coin_a_034,
        R.drawable.coin_a_035,
        R.drawable.coin_a_036,
        R.drawable.coin_a_037,
        R.drawable.coin_a_038,
        R.drawable.coin_a_039,
        R.drawable.coin_a_040,
        R.drawable.coin_a_041,
        R.drawable.coin_a_042,
        R.drawable.coin_a_043,
        R.drawable.coin_a_044,
        R.drawable.coin_a_045,
        R.drawable.coin_a_046,
        R.drawable.coin_a_047,
        R.drawable.coin_a_048,
        R.drawable.coin_a_049,
        R.drawable.coin_a_050,
        R.drawable.coin_a_051,
        R.drawable.coin_a_052,
        R.drawable.coin_a_053,
        R.drawable.coin_a_054,
        R.drawable.coin_a_055,
        R.drawable.coin_a_056,
        R.drawable.coin_a_057,
        R.drawable.coin_a_058,
        R.drawable.coin_a_059,
        R.drawable.coin_a_060,
        R.drawable.coin_a_061,
        R.drawable.coin_a_062,
        R.drawable.coin_a_063,
        R.drawable.coin_a_064,
        R.drawable.coin_a_065,
        R.drawable.coin_a_066,
        R.drawable.coin_a_067,
        R.drawable.coin_a_068,
        R.drawable.coin_a_069,
        R.drawable.coin_a_070,
        R.drawable.coin_a_071,
        R.drawable.coin_a_072,
        R.drawable.coin_a_073,
        R.drawable.coin_a_074,
        R.drawable.coin_a_075,
        R.drawable.coin_a_076,
        R.drawable.coin_a_077,
        R.drawable.coin_a_078,
        R.drawable.coin_a_079,
        R.drawable.coin_a_080,
        R.drawable.coin_a_081,
        R.drawable.coin_a_082,
        R.drawable.coin_a_083,
        R.drawable.coin_a_084,
        R.drawable.coin_a_085,
        R.drawable.coin_a_086,
        R.drawable.coin_a_087,
        R.drawable.coin_a_088,
        R.drawable.coin_a_089,
        R.drawable.coin_a_090,
        R.drawable.coin_a_091,
        R.drawable.coin_a_092,
        R.drawable.coin_a_093,
        R.drawable.coin_a_094,
        R.drawable.coin_a_095,
        R.drawable.coin_a_096,
        R.drawable.coin_a_097,
        R.drawable.coin_a_098,
        R.drawable.coin_a_099,
        R.drawable.coin_a_100,
        R.drawable.coin_a_101,
        R.drawable.coin_a_102,
        R.drawable.coin_a_103,
        R.drawable.coin_a_104,
        R.drawable.coin_a_105,
        R.drawable.coin_a_106,
        R.drawable.coin_a_107,
        R.drawable.coin_a_108,
        R.drawable.coin_a_109,
        R.drawable.coin_a_110,
        R.drawable.coin_a_111,
        R.drawable.coin_a_112,
        R.drawable.coin_a_113,
        R.drawable.coin_a_114,
        R.drawable.coin_a_115,
        R.drawable.coin_a_116,
        R.drawable.coin_a_117,
        R.drawable.coin_a_118,
        R.drawable.coin_a_119,
        R.drawable.coin_a_120,
    )
    private val resourceImgB: List<Int> = listOf(
        R.drawable.coin_b_001,
        R.drawable.coin_b_002,
        R.drawable.coin_b_003,
        R.drawable.coin_b_004,
        R.drawable.coin_b_005,
        R.drawable.coin_b_006,
        R.drawable.coin_b_007,
        R.drawable.coin_b_008,
        R.drawable.coin_b_009,
        R.drawable.coin_b_010,
        R.drawable.coin_b_011,
        R.drawable.coin_b_012,
        R.drawable.coin_b_013,
        R.drawable.coin_b_014,
        R.drawable.coin_b_015,
        R.drawable.coin_b_016,
        R.drawable.coin_b_017,
        R.drawable.coin_b_018,
        R.drawable.coin_b_019,
        R.drawable.coin_b_020,
        R.drawable.coin_b_021,
        R.drawable.coin_b_022,
        R.drawable.coin_b_023,
        R.drawable.coin_b_024,
        R.drawable.coin_b_025,
        R.drawable.coin_b_026,
        R.drawable.coin_b_027,
        R.drawable.coin_b_028,
        R.drawable.coin_b_029,
        R.drawable.coin_b_030,
        R.drawable.coin_b_031,
        R.drawable.coin_b_032,
        R.drawable.coin_b_033,
        R.drawable.coin_b_034,
        R.drawable.coin_b_035,
        R.drawable.coin_b_036,
        R.drawable.coin_b_037,
        R.drawable.coin_b_038,
        R.drawable.coin_b_039,
        R.drawable.coin_b_040,
        R.drawable.coin_b_041,
        R.drawable.coin_b_042,
        R.drawable.coin_b_043,
        R.drawable.coin_b_044,
        R.drawable.coin_b_045,
        R.drawable.coin_b_046,
        R.drawable.coin_b_047,
        R.drawable.coin_b_048,
        R.drawable.coin_b_049,
        R.drawable.coin_b_050,
        R.drawable.coin_b_051,
        R.drawable.coin_b_052,
        R.drawable.coin_b_053,
        R.drawable.coin_b_054,
        R.drawable.coin_b_055,
        R.drawable.coin_b_056,
        R.drawable.coin_b_057,
        R.drawable.coin_b_058,
        R.drawable.coin_b_059,
        R.drawable.coin_b_060,
        R.drawable.coin_b_061,
        R.drawable.coin_b_062,
        R.drawable.coin_b_063,
        R.drawable.coin_b_064,
        R.drawable.coin_b_065,
        R.drawable.coin_b_066,
        R.drawable.coin_b_067,
        R.drawable.coin_b_068,
        R.drawable.coin_b_069,
        R.drawable.coin_b_070,
        R.drawable.coin_b_071,
        R.drawable.coin_b_072,
        R.drawable.coin_b_073,
        R.drawable.coin_b_074,
        R.drawable.coin_b_075,
        R.drawable.coin_b_076,
        R.drawable.coin_b_077,
        R.drawable.coin_b_078,
        R.drawable.coin_b_079,
        R.drawable.coin_b_080,
        R.drawable.coin_b_081,
        R.drawable.coin_b_082,
        R.drawable.coin_b_083,
        R.drawable.coin_b_084,
        R.drawable.coin_b_085,
        R.drawable.coin_b_086,
        R.drawable.coin_b_087,
        R.drawable.coin_b_088,
        R.drawable.coin_b_089,
        R.drawable.coin_b_090,
        R.drawable.coin_b_091,
        R.drawable.coin_b_092,
        R.drawable.coin_b_093,
        R.drawable.coin_b_094,
        R.drawable.coin_b_095,
        R.drawable.coin_b_096,
        R.drawable.coin_b_097,
        R.drawable.coin_b_098,
        R.drawable.coin_b_099,
        R.drawable.coin_b_100,
        R.drawable.coin_b_101,
        R.drawable.coin_b_102,
        R.drawable.coin_b_103,
        R.drawable.coin_b_104,
        R.drawable.coin_b_105,
        R.drawable.coin_b_106,
        R.drawable.coin_b_107,
        R.drawable.coin_b_108,
        R.drawable.coin_b_109,
        R.drawable.coin_b_110,
        R.drawable.coin_b_111,
        R.drawable.coin_b_112,
        R.drawable.coin_b_113,
        R.drawable.coin_b_114,
        R.drawable.coin_b_115,
        R.drawable.coin_b_116,
        R.drawable.coin_b_117,
        R.drawable.coin_b_118,
        R.drawable.coin_b_119,
        R.drawable.coin_b_120,
    )
    private var baseWidth: Int = 0
    private var baseHeight: Int = 0
    private var boxSize: Int = 0
    private var marginLeft: Int = 0
    private var marginTop: Int = 0
    private var actionCount: Int = 0    //回転の制御
    private var destroyFlag: Boolean = false    //Activity破棄された場合など
    private var busyFlag: Boolean = false       //回転中
    private var itemRatios: Array<Int> = arrayOf(1,1)
    private var themeNumber: Int = 0
    private var localeLanguage: String = ""
    private var choiceCoin: Int = 0
    private var tossSpeed: Int = 10

    //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 THEME_NUMBER: String = "themeNumber"
        internal const val LOCALE_LANGUAGE: String = "localeLanguage"
        internal const val RATIO1: String = "ratio1"
        internal const val RATIO2: String = "ratio2"
        internal const val TOSS_SPEED: String = "tossSpeed"
    }

    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.loadItemStates()
        this.loadTossSpeed()
        //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.boxSize = minOf(this.baseWidth, this.baseHeight)
        this.marginLeft = (this.baseWidth - this.boxSize).div(2)
        this.marginTop = (this.baseHeight - this.boxSize).div(2)
        this.startButtonColor(true)
    }

    fun onClickStart(v: View) {
        if (this.busyFlag) {
            return
        }
        this.busyFlag = true
        this.startButtonColor(false)
        //選ぶ
        var ratioSum: Int = this.itemRatios[0] + this.itemRatios[1]
        if (ratioSum == 0) {
            this.itemRatios[0] = 1
            this.itemRatios[1] = 1
            ratioSum = 2
        }
        val choiceNum: Int = Random.nextInt(ratioSum)
        this.choiceCoin = if (choiceNum < this.itemRatios[0]) 0 else 1
        //
        this.destroyFlag = false
        this.actionCount = 0
        this.actionTimeline()
    }

    private fun actionTimeline() {
        Handler(Looper.getMainLooper()).postDelayed({
            if (this.destroyFlag) {
                this.startButtonColor(true)
                return@postDelayed
            }
            if (this.choiceCoin == 0) {
                this.binding.imageCoin.setImageResource(resourceImgA[this.actionCount])   //画像切り替え
            } else {
                this.binding.imageCoin.setImageResource(resourceImgB[this.actionCount])   //画像切り替え
            }
            this.actionCount += 1
            if (this.actionCount > 119) {
                if (this.destroyFlag) {
                    this.startButtonColor(true)
                    return@postDelayed
                }
                if (this.choiceCoin == 0) {
                    this.binding.imageCoin.setImageResource(resourceImgA[119])  //最後
                } else {
                    this.binding.imageCoin.setImageResource(resourceImgB[119])   //最後
                }
                this.busyFlag = false
                this.startButtonColor(true)
                return@postDelayed
            }
            actionTimeline()
        }, this.tossSpeed.toLong())
    }

    private fun startButtonColor(onOff: Boolean) {
        if (onOff) {
            this.binding.textStart.alpha = 1F
            this.binding.textSetting.alpha = 1F
        } else {
            this.binding.textStart.alpha = 0F
            this.binding.textSetting.alpha = 0F
        }
    }

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

    fun onClickSetting(v: View) {
        if (this.busyFlag) {
            return
        }
        this.destroyFlag = true
        val intent = Intent(applicationContext, SettingActivity::class.java)
        intent.putExtra(RATIO1, this.itemRatios[0])
        intent.putExtra(RATIO2, this.itemRatios[1])
        intent.putExtra(TOSS_SPEED, this.tossSpeed)
        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 lastTossSpeed: Int = this.tossSpeed
            val lastThemeNumber = this.themeNumber
            this.itemRatios = arrayOf(
                intent.getIntExtra(RATIO1, 0),
                intent.getIntExtra(RATIO2, 0),
            )
            this.saveItemStates()
            this.tossSpeed = intent.getIntExtra(TOSS_SPEED, 0)
            if (this.tossSpeed != lastTossSpeed) {
                this.saveTossSpeed()
            }
            this.themeNumber = intent.getIntExtra(THEME_NUMBER, 0)
            if (this.themeNumber != lastThemeNumber) {
                this.saveThemeNumber()
            }
            val lastLocaleLanguage = this.localeLanguage
            this.localeLanguage = intent.getStringExtra(LOCALE_LANGUAGE) ?: ""
            if (this.localeLanguage != lastLocaleLanguage) {
                this.saveLocaleLanguage()
            }
        }
        recreate()
    }

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

    //itemsを保存
    private fun saveItemStates() {
        getSharedPreferences(SETTINGS, Context.MODE_PRIVATE).edit().apply {
            putInt(RATIO1, this@MainActivity.itemRatios[0])
            putInt(RATIO2, this@MainActivity.itemRatios[1])
            apply()
        }
    }

    //itemsを読み出し
    private fun loadItemStates() {
        val pref = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
        this.itemRatios = arrayOf(
                pref.getInt(RATIO1, 0),
                pref.getInt(RATIO2, 0),
        )
        if (this.itemRatios[0] == 0 && this.itemRatios[1] == 0) {
            this.itemRatios[0] = 1
            this.itemRatios[1] = 1
            this.saveItemStates()
        }
    }

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

    //toss speedを保存
    private fun saveTossSpeed() {
        getSharedPreferences(SETTINGS, Context.MODE_PRIVATE).edit().apply {
            putInt(TOSS_SPEED, this@MainActivity.tossSpeed)
            apply()
        }
    }

    //toss speedを読み出し
    private fun loadTossSpeed() {
        val pref = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
        this.tossSpeed = pref.getInt(TOSS_SPEED, 10)
        if (this.tossSpeed <= 0) {
            this.tossSpeed = 1
        } else if (this.tossSpeed > 1000) {
            this.tossSpeed = 1000
        }
    }

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

    //テーマを保存
    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/twosidesofcoin/SettingActivity.kt

package jp.aosystem.twosidesofcoin

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 android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import jp.aosystem.twosidesofcoin.databinding.ActivitySettingBinding
import java.util.*

class SettingActivity : AppCompatActivity() {
    private lateinit var binding: ActivitySettingBinding
    private lateinit var viewModel: ConstraintLayout
    private var inputMethodManager: InputMethodManager? = null

    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 itemRatio1: Int = intent.getIntExtra(MainActivity.RATIO1,0)
        val itemRatio2: Int = intent.getIntExtra(MainActivity.RATIO2,0)
        val tossSpeed: Int = intent.getIntExtra(MainActivity.TOSS_SPEED,10)
        val themeNumber: Int = intent.getIntExtra(MainActivity.THEME_NUMBER,0)
        if (itemRatio1 != 0) {
            this.binding.editRatio1.setText(itemRatio1.toString())
        }
        if (itemRatio2 != 0) {
            this.binding.editRatio2.setText(itemRatio2.toString())
        }
        if (tossSpeed != 0) {
            this.binding.editTossSpeed.setText(tossSpeed.toString())
        }
        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()    //タイトルバー非表示
        //
        this.setTouchListener()
    }

    //背景タッチでテキストエリアからフォーカスを外してキーボードを隠す
    private fun setTouchListener() {
        this.inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
        this.binding.editRatio1.setOnFocusChangeListener { _, hasFocus ->
            if (!hasFocus) {
                this.inputMethodManager!!.hideSoftInputFromWindow(   //キーボードを隠す
                    this.binding.editRatio1.windowToken,
                    InputMethodManager.HIDE_NOT_ALWAYS
                )
            }
        }
        this.binding.editRatio2.setOnFocusChangeListener { _, hasFocus ->
            if (!hasFocus) {
                this.inputMethodManager!!.hideSoftInputFromWindow(   //キーボードを隠す
                    this.binding.editRatio2.windowToken,
                    InputMethodManager.HIDE_NOT_ALWAYS
                )
            }
        }
        this.binding.editTossSpeed.setOnFocusChangeListener { _, hasFocus ->
            if (!hasFocus) {
                this.inputMethodManager!!.hideSoftInputFromWindow(   //キーボードを隠す
                        this.binding.editTossSpeed.windowToken,
                        InputMethodManager.HIDE_NOT_ALWAYS
                )
            }
        }
    }

    fun onClickApply(v: View) {
        val intent = Intent()
        val itemRatio1: Int = ("0" + this.binding.editRatio1.text.toString()).toInt()
        intent.putExtra(MainActivity.RATIO1, itemRatio1)
        val itemRatio2: Int = ("0" + this.binding.editRatio2.text.toString()).toInt()
        intent.putExtra(MainActivity.RATIO2, itemRatio2)
        val tossSpeed: Int = ("0" + this.binding.editTossSpeed.text.toString()).toInt()
        intent.putExtra(MainActivity.TOSS_SPEED, tossSpeed)
        val themeNumber = if (this.binding.switchTheme.isChecked) 1 else 0
        intent.putExtra(MainActivity.THEME_NUMBER, themeNumber)
        var localeLanguage: String = ""
        if (this.binding.radioLanguageEn.isChecked) {
            localeLanguage = "en"
        } else if (this.binding.radioLanguageJa.isChecked) {
            localeLanguage = "ja"
        }
        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:background="#9c8443"
        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="9"
                android:background="#FFC107"
                android:gravity="center"
                android:minHeight="96dip"
                android:onClick="onClickStart"
                android:text="@string/start" />

            <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="#FF9800"
                android:gravity="center"
                android:minHeight="96dip"
                android:onClick="onClickSetting"
                android:text="@string/setting" />

        </LinearLayout>

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

            <ImageView
                android:id="@+id/imageCoin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/app_name"
                app:srcCompat="@drawable/coin_a_001" />

        </FrameLayout>

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

    </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="48dp"
        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="#5DC6AF"
            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="#5FB1C6"
            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: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/textRatio"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/ratio"
                android:textColor="?android:attr/textColorPrimary" />

            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp">

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/textRatio1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/ratioA" />

                    <EditText
                        android:id="@+id/editRatio1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_weight="1"
                        android:ems="6"
                        android:inputType="number"
                        tools:ignore="Autofill" />
                </TableRow>

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/textRatio2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/ratioB" />

                    <EditText
                        android:id="@+id/editRatio2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_weight="1"
                        android:ems="6"
                        android:inputType="number"
                        tools:ignore="Autofill" />
                </TableRow>

            </TableLayout>


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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp"
                android:orientation="horizontal">

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

                <EditText
                    android:id="@+id/editTossSpeed"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_weight="1"
                    android:ems="6"
                    android:inputType="number"
                    tools:ignore="Autofill" />

            </LinearLayout>

            <TextView
                android:id="@+id/textTossSpeed2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/tossSpeed2"
                android:textColor="?android:attr/textColorSecondary" />

            <View
                android:id="@+id/divider2"
                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/divider5"
                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/divider3"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="15dp"
                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" />

            <TextView
                android:id="@+id/textUsage1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/usage1" />

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

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

        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
このページでお気付きの点がございましたら
ご連絡いただけますと幸いです。
Eメール(任意)
送信内容
 

■ 便利ウェブサイト