ソースコード source code

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

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

下記コードの最終ビルド日: 2022-05-20

build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.1.3' apply false
    id 'com.android.library' version '7.1.3' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

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

app/build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "jp.aosystem.onestroke"
        minSdk 21
        targetSdk 31
        versionCode 3
        versionName "1.2"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

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

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'com.google.android.gms:play-services-ads:20.6.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.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.onestroke">

    <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.OneStroke">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SettingActivity"
            android:exported="false" />
        <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/onestroke/MainActivity.kt

package jp.aosystem.onestroke

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.media.AudioAttributes
import android.media.SoundPool
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.LocaleList
import android.util.DisplayMetrics
import android.webkit.WebSettings
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatDelegate
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 java.util.*

class MainActivity : AppCompatActivity() {
    //view
    private lateinit var vAdContainer: LinearLayout
    private lateinit var vTextStart: TextView
    private lateinit var vTextReset: TextView
    private lateinit var vTextAnswer: TextView
    private lateinit var vTextSetting: TextView
    private lateinit var vBoxWebView: BoxWebView
    //sound
    private lateinit var soundPool: SoundPool
    private var soundStart = 0
    private var soundRetry = 0
    //
    private var destroyFlag: Boolean = false    //Activity破棄された場合など
    private var themeNumber: Int = 0
    private var localeLanguage: String = ""
    private var puzzleLevel: Int = 4
    private var soundEnable: Int = 1    //1|0
    private var modelAnswer: 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.vAdContainer.width.toFloat()
            if (adWidthPixels == 0f) {
                adWidthPixels = outMetrics.widthPixels.toFloat()
            }
            val adWidth = (adWidthPixels / density).toInt()
            return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
        }

    companion object {
        //internal const val AD_UNIT_ID: String = "ca-app-pub-3940256099942544/6300978111"     //adMob Test
        internal const val AD_UNIT_ID: String = "ca-app-pub-0000000000000000/0000000000"
        internal const val MODEL_ANSWER_INIT: Int = 10
        internal const val SETTINGS: String = "settings"
        internal const val THEME_NUMBER: String = "themeNumber"
        internal const val LOCALE_LANGUAGE: String = "localeLanguage"
        internal const val PUZZLE_LEVEL: String = "puzzleLevel"
        internal const val SOUND_ENABLE: String = "soundEnable"
    }

    private val settingStartForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
        if (result.resultCode == Activity.RESULT_OK) {
            val intent: Intent? = result.data
            if (intent != null) {
                val lastThemeNumber: Int = 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()
                }
                val lastPuzzleLevel = this.puzzleLevel
                this.puzzleLevel = intent.getIntExtra(PUZZLE_LEVEL,4)
                if (this.puzzleLevel != lastPuzzleLevel) {
                    this.savePuzzleLevel()
                }
                val lastSoundEnable = this.soundEnable
                this.soundEnable = intent.getIntExtra(SOUND_ENABLE,1)
                if (this.soundEnable != lastSoundEnable) {
                    this.saveSoundEnable()
                }
                recreate()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //タイトルバー非表示
        supportActionBar?.hide()
        //
        this.vAdContainer = findViewById(R.id.ad_container)
        this.vTextStart = findViewById(R.id.textStart)
        this.vTextReset = findViewById(R.id.textReset)
        this.vTextAnswer = findViewById(R.id.textAnswer)
        this.vTextSetting = findViewById(R.id.textSetting)
        this.vBoxWebView = findViewById(R.id.boxWebView)
        //テーマ読み込みと設定
        this.loadThemeNumber()
        this.setTheme()
        //読み込み
        this.loadPuzzleLevel()
        this.loadSoundEnable()
        //sound
        val audioAttributes = AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_GAME)
            .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
            .build()
        this.soundPool = SoundPool.Builder()
            .setAudioAttributes(audioAttributes)
            .setMaxStreams(2)
            .build()
        this.soundStart = soundPool.load(this, R.raw.start, 1)
        this.soundRetry = soundPool.load(this, R.raw.retry, 1)
        //onClickListener
        this.vTextStart.setOnClickListener {
            this.onClickStart()
        }
        this.vTextReset.setOnClickListener {
            this.onClickReset()
        }
        this.vTextAnswer.setOnClickListener {
            this.onClickAnswer()
        }
        this.vTextSetting.setOnClickListener {
            this.onClickSetting()
        }
        //adMob
        MobileAds.initialize(this) {}
        this.adView = AdView(this)
        this.vAdContainer.addView(this.adView)
        this.loadBanner()
    }

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

    override fun onResume() {
        super.onResume()
        this.vTextReset.text = getString(R.string.reset,":" + this.modelAnswer.toString())
        this.vTextAnswer.text = ""
        val locale: String = this.localeLanguage
        val puzzleLevel: String = this.puzzleLevel.toString()
        val soundEnable: String = this.soundEnable.toString()
        val webSettings: WebSettings = this.vBoxWebView.settings
        webSettings.javaScriptEnabled = true    //setJavaScriptEnabled を使用すると、アプリケーションに XSS の脆弱性が侵入する可能性があります。と表示されているだけ。
        webSettings.domStorageEnabled = true    //LocalStorage使用
        webSettings.mediaPlaybackRequiresUserGesture = false    //Audio使用
        this.vBoxWebView.loadUrl("file:///android_asset/web/start.html?locale=$locale&p=$puzzleLevel&r=0&s=$soundEnable")
    }

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

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

    private fun onClickStart() {
        this.destroyFlag = false
        this.game()
    }

    private fun onClickReset() {
        this.destroyFlag = false
        this.gameReset()
    }

    private fun onClickAnswer() {
        this.destroyFlag = false
        this.gameAnswer()
    }

    private fun onClickSetting() {
        this.destroyFlag = true
        this.modelAnswer = MODEL_ANSWER_INIT
        val intent = Intent(applicationContext, SettingActivity::class.java)
        intent.putExtra(THEME_NUMBER, this.themeNumber)
        intent.putExtra(PUZZLE_LEVEL, this.puzzleLevel)
        intent.putExtra(SOUND_ENABLE, this.soundEnable)
        intent.putExtra(LOCALE_LANGUAGE, this.localeLanguage)
        this.settingStartForResult.launch(intent)
    }

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

    private fun game() {
        this.modelAnswer = MODEL_ANSWER_INIT
        this.vTextReset.text = getString(R.string.reset,":" + this.modelAnswer.toString())
        this.vTextAnswer.text = ""
        if (this.soundEnable == 1) {
            this.soundPool.play(this.soundStart, 1.0f, 1.0f, 0, 0, 1.0f)
        }
        //this.vBoxWebView.clearCache(true)     //途中で音が出なくなるので記述したが効果なし
        val locale: String = this.localeLanguage
        val puzzleLevel: String = this.puzzleLevel.toString()
        val soundEnable: String = this.soundEnable.toString()
        val webSettings: WebSettings = this.vBoxWebView.settings
        webSettings.javaScriptEnabled = true    //setJavaScriptEnabled を使用すると、アプリケーションに XSS の脆弱性が侵入する可能性があります。と表示されているだけ。
        webSettings.domStorageEnabled = true    //LocalStorage使用
        webSettings.mediaPlaybackRequiresUserGesture = false    //Audio使用
        //webSettings.cacheMode = WebSettings.LOAD_CACHE_ONLY //WebSettings.LOAD_NO_CACHE
        this.vBoxWebView.loadUrl("file:///android_asset/web/index.html?locale=$locale&p=$puzzleLevel&r=0&a=0&s=$soundEnable")
    }

    private fun gameReset() {
        this.modelAnswer -= 1
        if (this.modelAnswer > 0) {
            this.vTextReset.text = getString(R.string.reset, ":" + this.modelAnswer.toString())
            this.vTextAnswer.text = ""
        } else {
            this.modelAnswer = 0
            this.vTextReset.text = getString(R.string.reset,"")
            this.vTextAnswer.text = getString(R.string.answer)
        }
        if (this.soundEnable == 1) {
            this.soundPool.play(this.soundRetry, 1.0f, 1.0f, 0, 0, 1.0f)
        }
        val locale: String = this.localeLanguage
        val puzzleLevel: String = this.puzzleLevel.toString()
        val soundEnable: String = this.soundEnable.toString()
        val webSettings: WebSettings = this.vBoxWebView.settings
        webSettings.javaScriptEnabled = true    //setJavaScriptEnabled を使用すると、アプリケーションに XSS の脆弱性が侵入する可能性があります。と表示されているだけ。
        webSettings.domStorageEnabled = true    //LocalStorage使用
        webSettings.mediaPlaybackRequiresUserGesture = false    //Audio使用
        //webSettings.cacheMode = WebSettings.LOAD_CACHE_ONLY //WebSettings.LOAD_NO_CACHE
        this.vBoxWebView.loadUrl("file:///android_asset/web/index.html?locale=$locale&p=$puzzleLevel&r=1&a=0&s=$soundEnable")
    }

    private fun gameAnswer() {
        if (this.modelAnswer > 0) {
            return
        }
        if (this.soundEnable == 1) {
            this.soundPool.play(this.soundRetry, 1.0f, 1.0f, 0, 0, 1.0f)
        }
        val locale: String = this.localeLanguage
        val puzzleLevel: String = this.puzzleLevel.toString()
        val soundEnable: String = this.soundEnable.toString()
        val webSettings: WebSettings = this.vBoxWebView.settings
        webSettings.javaScriptEnabled = true    //setJavaScriptEnabled を使用すると、アプリケーションに XSS の脆弱性が侵入する可能性があります。と表示されているだけ。
        webSettings.domStorageEnabled = true    //LocalStorage使用
        webSettings.mediaPlaybackRequiresUserGesture = false    //Audio使用
        //webSettings.cacheMode = WebSettings.LOAD_CACHE_ONLY //WebSettings.LOAD_NO_CACHE
        this.vBoxWebView.loadUrl("file:///android_asset/web/index.html?locale=$locale&p=$puzzleLevel&r=0&a=1&s=$soundEnable")
    }

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

    //テーマを保存
    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)
        }
    }

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

    //PuzzleLevelを保存
    private fun savePuzzleLevel() {
        getSharedPreferences(SETTINGS, Context.MODE_PRIVATE).edit().apply {
            putInt(PUZZLE_LEVEL, this@MainActivity.puzzleLevel)
            apply()
        }
    }

    //PuzzleLevelを読み出し
    private fun loadPuzzleLevel() {
        val pref = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
        this.puzzleLevel = pref.getInt(PUZZLE_LEVEL, 4)
    }

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

    //SoundEnableを保存
    private fun saveSoundEnable() {
        getSharedPreferences(SETTINGS, Context.MODE_PRIVATE).edit().apply {
            putInt(SOUND_ENABLE, this@MainActivity.soundEnable)
            apply()
        }
    }

    //SoundEnableを読み出し
    private fun loadSoundEnable() {
        val pref = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
        this.soundEnable = pref.getInt(SOUND_ENABLE, 1)
    }

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

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

    //localeLanguageを読み出し
    private fun loadLocaleLanguage() {
        val pref = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
        this.localeLanguage = pref.getString(LOCALE_LANGUAGE, "") ?: ""
    }

    //言語設定
    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/onestroke/SettingActivity.kt

package jp.aosystem.onestroke

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.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SwitchCompat
import java.util.*

class SettingActivity : AppCompatActivity() {

    private lateinit var vTextApply: TextView
    private lateinit var vTextCancel: TextView
    private lateinit var vSwitchTheme: SwitchCompat
    private lateinit var vSpinnerLevel: Spinner
    private lateinit var vSwitchSoundEnable: SwitchCompat
    private lateinit var vRadioLanguageSystem: RadioButton
    private lateinit var vRadioLanguageEn: RadioButton
    private lateinit var vRadioLanguageBg: RadioButton
    private lateinit var vRadioLanguageCs: RadioButton
    private lateinit var vRadioLanguageDa: RadioButton
    private lateinit var vRadioLanguageDe: RadioButton
    private lateinit var vRadioLanguageEl: RadioButton
    private lateinit var vRadioLanguageEs: RadioButton
    private lateinit var vRadioLanguageEt: RadioButton
    private lateinit var vRadioLanguageFi: RadioButton
    private lateinit var vRadioLanguageFr: RadioButton
    private lateinit var vRadioLanguageHu: RadioButton
    private lateinit var vRadioLanguageIt: RadioButton
    private lateinit var vRadioLanguageJa: RadioButton
    private lateinit var vRadioLanguageLt: RadioButton
    private lateinit var vRadioLanguageLv: RadioButton
    private lateinit var vRadioLanguageNl: RadioButton
    private lateinit var vRadioLanguagePl: RadioButton
    private lateinit var vRadioLanguagePt: RadioButton
    private lateinit var vRadioLanguageRo: RadioButton
    private lateinit var vRadioLanguageRu: RadioButton
    private lateinit var vRadioLanguageSk: RadioButton
    private lateinit var vRadioLanguageSv: RadioButton
    private lateinit var vRadioLanguageTh: RadioButton
    private lateinit var vRadioLanguageZh: RadioButton

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting)
        //タイトルバー非表示
        supportActionBar?.hide()
        //
        this.vTextApply = findViewById(R.id.textApply)
        this.vTextCancel = findViewById(R.id.textCancel)
        this.vSwitchTheme = findViewById(R.id.switchTheme)
        this.vSpinnerLevel = findViewById(R.id.spinnerLevel)
        this.vSwitchSoundEnable = findViewById(R.id.switchSoundEnable)
        this.vRadioLanguageSystem = findViewById(R.id.radioLanguageSystem)
        this.vRadioLanguageEn = findViewById(R.id.radioLanguageEn)
        this.vRadioLanguageBg = findViewById(R.id.radioLanguageBg)
        this.vRadioLanguageCs = findViewById(R.id.radioLanguageCs)
        this.vRadioLanguageDa = findViewById(R.id.radioLanguageDa)
        this.vRadioLanguageDe = findViewById(R.id.radioLanguageDe)
        this.vRadioLanguageEl = findViewById(R.id.radioLanguageEl)
        this.vRadioLanguageEs = findViewById(R.id.radioLanguageEs)
        this.vRadioLanguageEt = findViewById(R.id.radioLanguageEt)
        this.vRadioLanguageFi = findViewById(R.id.radioLanguageFi)
        this.vRadioLanguageFr = findViewById(R.id.radioLanguageFr)
        this.vRadioLanguageHu = findViewById(R.id.radioLanguageHu)
        this.vRadioLanguageIt = findViewById(R.id.radioLanguageIt)
        this.vRadioLanguageJa = findViewById(R.id.radioLanguageJa)
        this.vRadioLanguageLt = findViewById(R.id.radioLanguageLt)
        this.vRadioLanguageLv = findViewById(R.id.radioLanguageLv)
        this.vRadioLanguageNl = findViewById(R.id.radioLanguageNl)
        this.vRadioLanguagePl = findViewById(R.id.radioLanguagePl)
        this.vRadioLanguagePt = findViewById(R.id.radioLanguagePt)
        this.vRadioLanguageRo = findViewById(R.id.radioLanguageRo)
        this.vRadioLanguageRu = findViewById(R.id.radioLanguageRu)
        this.vRadioLanguageSk = findViewById(R.id.radioLanguageSk)
        this.vRadioLanguageSv = findViewById(R.id.radioLanguageSv)
        this.vRadioLanguageTh = findViewById(R.id.radioLanguageTh)
        this.vRadioLanguageZh = findViewById(R.id.radioLanguageZh)
        //
        //spinner
        this.setSpinnerLevel()
        //データ受け取り
        val intent = this.intent
        val themeNumber: Int = intent.getIntExtra(MainActivity.THEME_NUMBER,0)
        this.vSwitchTheme.isChecked = themeNumber != 0
        val puzzleLevel: Int = intent.getIntExtra(MainActivity.PUZZLE_LEVEL,4)
        this.vSpinnerLevel.setSelection(puzzleLevel - 4)    //4..20
        val soundEnable: Int = intent.getIntExtra(MainActivity.SOUND_ENABLE,1)
        this.vSwitchSoundEnable.isChecked = soundEnable != 0
        val localeLanguage: String = intent.getStringExtra(MainActivity.LOCALE_LANGUAGE) ?: ""
        when (localeLanguage) {
            "en" -> this.vRadioLanguageEn.isChecked = true
            "bg" -> this.vRadioLanguageBg.isChecked = true
            "cs" -> this.vRadioLanguageCs.isChecked = true
            "da" -> this.vRadioLanguageDa.isChecked = true
            "de" -> this.vRadioLanguageDe.isChecked = true
            "el" -> this.vRadioLanguageEl.isChecked = true
            "es" -> this.vRadioLanguageEs.isChecked = true
            "et" -> this.vRadioLanguageEt.isChecked = true
            "fi" -> this.vRadioLanguageFi.isChecked = true
            "fr" -> this.vRadioLanguageFr.isChecked = true
            "hu" -> this.vRadioLanguageHu.isChecked = true
            "it" -> this.vRadioLanguageIt.isChecked = true
            "ja" -> this.vRadioLanguageJa.isChecked = true
            "lt" -> this.vRadioLanguageLt.isChecked = true
            "lv" -> this.vRadioLanguageLv.isChecked = true
            "nl" -> this.vRadioLanguageNl.isChecked = true
            "pl" -> this.vRadioLanguagePl.isChecked = true
            "pt" -> this.vRadioLanguagePt.isChecked = true
            "ro" -> this.vRadioLanguageRo.isChecked = true
            "ru" -> this.vRadioLanguageRu.isChecked = true
            "sk" -> this.vRadioLanguageSk.isChecked = true
            "sv" -> this.vRadioLanguageSv.isChecked = true
            "th" -> this.vRadioLanguageTh.isChecked = true
            "zh" -> this.vRadioLanguageZh.isChecked = true
            else -> this.vRadioLanguageSystem.isChecked = true
        }
        //onClickListener
        this.vTextApply.setOnClickListener {
            this.onClickApply()
        }
        this.vTextCancel.setOnClickListener {
            this.onClickCancel()
        }
    }

    private fun setSpinnerLevel() {
        val adapter1 = ArrayAdapter<String>(this, android.R.layout.simple_spinner_item)
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        for (i in 4..20) {
            adapter1.add(i.toString())
        }
        this.vSpinnerLevel.adapter = adapter1
        this.vSpinnerLevel.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            // アイテムが選択された時
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            }
            // アイテムが選択されなかった
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }
    }

    private fun onClickApply() {
        val themeNumber: Int = if (this.vSwitchTheme.isChecked) 1 else 0
        val puzzleLevel: Int = this.vSpinnerLevel.selectedItem.toString().toInt()
        val soundEnable: Int = if (this.vSwitchSoundEnable.isChecked) 1 else 0
        var localeLanguage: String = ""
        if (this.vRadioLanguageEn.isChecked) {
            localeLanguage = "en"
        } else if (this.vRadioLanguageBg.isChecked) {
            localeLanguage = "bg"
        } else if (this.vRadioLanguageCs.isChecked) {
            localeLanguage = "cs"
        } else if (this.vRadioLanguageDa.isChecked) {
            localeLanguage = "da"
        } else if (this.vRadioLanguageDe.isChecked) {
            localeLanguage = "de"
        } else if (this.vRadioLanguageEl.isChecked) {
            localeLanguage = "el"
        } else if (this.vRadioLanguageEs.isChecked) {
            localeLanguage = "es"
        } else if (this.vRadioLanguageEt.isChecked) {
            localeLanguage = "et"
        } else if (this.vRadioLanguageFi.isChecked) {
            localeLanguage = "fi"
        } else if (this.vRadioLanguageFr.isChecked) {
            localeLanguage = "fr"
        } else if (this.vRadioLanguageHu.isChecked) {
            localeLanguage = "hu"
        } else if (this.vRadioLanguageIt.isChecked) {
            localeLanguage = "it"
        } else if (this.vRadioLanguageJa.isChecked) {
            localeLanguage = "ja"
        } else if (this.vRadioLanguageLt.isChecked) {
            localeLanguage = "lt"
        } else if (this.vRadioLanguageLv.isChecked) {
            localeLanguage = "lv"
        } else if (this.vRadioLanguageNl.isChecked) {
            localeLanguage = "nl"
        } else if (this.vRadioLanguagePl.isChecked) {
            localeLanguage = "pl"
        } else if (this.vRadioLanguagePt.isChecked) {
            localeLanguage = "pt"
        } else if (this.vRadioLanguageRo.isChecked) {
            localeLanguage = "ro"
        } else if (this.vRadioLanguageRu.isChecked) {
            localeLanguage = "ru"
        } else if (this.vRadioLanguageSk.isChecked) {
            localeLanguage = "sk"
        } else if (this.vRadioLanguageSv.isChecked) {
            localeLanguage = "sv"
        } else if (this.vRadioLanguageTh.isChecked) {
            localeLanguage = "th"
        } else if (this.vRadioLanguageZh.isChecked) {
            localeLanguage = "zh"
        }
        val intent = Intent()
        intent.putExtra(MainActivity.THEME_NUMBER, themeNumber)
        intent.putExtra(MainActivity.PUZZLE_LEVEL, puzzleLevel)
        intent.putExtra(MainActivity.SOUND_ENABLE, soundEnable)
        intent.putExtra(MainActivity.LOCALE_LANGUAGE, localeLanguage)
        setResult(Activity.RESULT_OK, intent)
        finish()
    }

    private fun onClickCancel() {
        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/java/jp/aosystem/onestroke/BoxWebView.kt

package jp.aosystem.onestroke

import android.content.Context
import android.util.AttributeSet
import android.webkit.WebView

class BoxWebView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : WebView(context, attrs) {

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val width = measuredWidth
        setMeasuredDimension(width, width)
    }
}

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: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="48dp"
            android:layout_marginStart="1dp"
            android:layout_marginTop="1dp"
            android:layout_marginEnd="1dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textStart"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:background="@color/main_start_bg"
                android:gravity="center"
                android:text="@string/start"
                android:textColor="@color/white"
                tools:ignore="TextContrastCheck" />

            <TextView
                android:id="@+id/textReset"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginStart="1dp"
                android:layout_weight="2"
                android:background="@color/main_start_bg"
                android:gravity="center"
                android:text="@string/reset"
                android:textColor="@color/white"
                tools:ignore="TextContrastCheck" />

            <TextView
                android:id="@+id/textAnswer"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginStart="1dp"
                android:layout_weight="1"
                android:background="@color/main_start_bg"
                android:gravity="center"
                android:text="@string/answer"
                android:textColor="@color/white"
                tools:ignore="TextContrastCheck" />

            <TextView
                android:id="@+id/textSetting"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginStart="1dp"
                android:layout_weight="1"
                android:background="@color/main_setting_bg"
                android:gravity="center"
                android:text="@string/setting"
                android:textColor="@color/white"
                tools:ignore="TextContrastCheck" />

        </LinearLayout>

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

            <Space
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <jp.aosystem.onestroke.BoxWebView
                android:id="@+id/boxWebView"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </jp.aosystem.onestroke.BoxWebView>

            <Space
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="2" />

        </LinearLayout>


    </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: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/layoutButtons"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textCancel"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@color/setting_cancel_bg"
            android:gravity="center"
            android:text="@string/cancel"
            android:textColor="@color/white"
            tools:ignore="TextContrastCheck" />

        <TextView
            android:id="@+id/textApply"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="1dp"
            android:layout_weight="1"
            android:background="@color/setting_apply_bg"
            android:gravity="center"
            android:text="@string/apply"
            android:textColor="@color/white"
            tools:ignore="TextContrastCheck" />

    </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:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="@string/usage" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="@string/usage1" />

            <View
                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:orientation="horizontal"
                android:paddingTop="24dp"
                android:paddingBottom="10dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="3"
                    android:text="@string/level"
                    android:textColor="?android:attr/textColorPrimary" />

                <Spinner
                    android:id="@+id/spinnerLevel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:contentDescription="@string/level" />
            </LinearLayout>

            <View
                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/switchSoundEnable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:text="@string/sound" />

            <View
                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:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="20dp"
                android:background="?android:attr/listDivider" />

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="@string/languageNote" />

            <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/radioLanguageBg"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageBg" />
                <RadioButton
                    android:id="@+id/radioLanguageCs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageCs" />
                <RadioButton
                    android:id="@+id/radioLanguageDa"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageDa" />
                <RadioButton
                    android:id="@+id/radioLanguageDe"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageDe" />
                <RadioButton
                    android:id="@+id/radioLanguageEl"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageEl" />
                <RadioButton
                    android:id="@+id/radioLanguageEs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageEs" />
                <RadioButton
                    android:id="@+id/radioLanguageEt"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageEt" />
                <RadioButton
                    android:id="@+id/radioLanguageFi"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageFi" />
                <RadioButton
                    android:id="@+id/radioLanguageFr"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageFr" />
                <RadioButton
                    android:id="@+id/radioLanguageHu"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageHu" />
                <RadioButton
                    android:id="@+id/radioLanguageIt"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageIt" />
                <RadioButton
                    android:id="@+id/radioLanguageJa"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageJa" />
                <RadioButton
                    android:id="@+id/radioLanguageLt"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageLt" />
                <RadioButton
                    android:id="@+id/radioLanguageLv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageLv" />
                <RadioButton
                    android:id="@+id/radioLanguageNl"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageNl" />
                <RadioButton
                    android:id="@+id/radioLanguagePl"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languagePl" />
                <RadioButton
                    android:id="@+id/radioLanguagePt"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languagePt" />
                <RadioButton
                    android:id="@+id/radioLanguageRo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageRo" />
                <RadioButton
                    android:id="@+id/radioLanguageRu"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageRu" />
                <RadioButton
                    android:id="@+id/radioLanguageSk"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageSk" />
                <RadioButton
                    android:id="@+id/radioLanguageSv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageSv" />
                <RadioButton
                    android:id="@+id/radioLanguageTh"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageTh" />
                <RadioButton
                    android:id="@+id/radioLanguageZh"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/languageZh" />
            </RadioGroup>


            <View
                android:id="@+id/divider1"
                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>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
このページでお気付きの点がございましたら
ご連絡いただけますと幸いです。
Eメール(任意)
送信内容
 

便利ウェブサイト