ラベル Kotlin の投稿を表示しています。 すべての投稿を表示
ラベル Kotlin の投稿を表示しています。 すべての投稿を表示

2026年3月20日金曜日

Android アプリ Jetpack Compose で動的言語切り替え

Kotlin +  Jetpack Compose の Android アプリの開発で、ユーザの選択した言語でが切り替わる機能を実装しようと調べたところ、AppCompatActivity を使用するのが簡単な模様。

まずは対応する言語の一覧の設定ファイルを作成。

res/xml/locales_config.xml

<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
    <locale android:name="ja" />
    <locale android:name="en" />
</locale-config>


次に各言語の文言を作成。デフォルトから。

res/values/strings.xml

<resources>
    <string name="app_name">こんにちは 世界</string>
</resources>

values-{言語コード} を作成。

res/values-en/strings.xml

<resources>
    <string name="app_name">Hello World</string>
</resources>


そして AppCompatActivity を継承するように変更。

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        [snip]
        
    }
}


表示箇所はこんな感じ。

Text(stringResource(R.string.app_name))


切り替えるコードは AppCompatDelegate.setApplicationLocales を使用。適当にトグルする。

onClick = {
    val current = AppCompatDelegate.getApplicationLocales()
    val index = current.indexOf(Locale.ENGLISH)
    val localeList: LocaleListCompat = LocaleListCompat.forLanguageTags(if (index != 0) "en" else "ja")
    AppCompatDelegate.setApplicationLocales(localeList)
}


エミュレータで実行すると・・・エラー・・・

FATAL EXCEPTION: main
    Process: com.example.hello_world, PID: 21425
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hello_world/com.example.hello_world.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3645)
    	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782)
    	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
    	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138)
    	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
    	at android.os.Handler.dispatchMessage(Handler.java:106)
    	at android.os.Looper.loopOnce(Looper.java:201)
    	at android.os.Looper.loop(Looper.java:288)
    	at android.app.ActivityThread.main(ActivityThread.java:7924)
    	at java.lang.reflect.Method.invoke(Native Method)
    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
    Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    	at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:902)
    	at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:865)
    	at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:757)
    	at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:205)
    	at androidx.activity.compose.ComponentActivityKt.setContent(ComponentActivity.kt:70)
    	at androidx.activity.compose.ComponentActivityKt.setContent$default(ComponentActivity.kt:51)
    	at com.example.hello_world.MainActivity.onCreate(MainActivity.kt:67)
    	at android.app.Activity.performCreate(Activity.java:8342)
    	at android.app.Activity.performCreate(Activity.java:8321)
    	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417)
    	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3626)
    	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3782) 
    	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) 
    	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138) 
    	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307) 
    	at android.os.Handler.dispatchMessage(Handler.java:106) 
    	at android.os.Looper.loopOnce(Looper.java:201) 
    	at android.os.Looper.loop(Looper.java:288) 
    	at android.app.ActivityThread.main(ActivityThread.java:7924) 
    	at java.lang.reflect.Method.invoke(Native Method) 
    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 


エラー文言で調べてみると AndroidManifest.xml の android:theme を変えればいいらしい。

<application> 属性の android:theme を変えてみるが赤く光る・・・


<activity> 属性のほうを変えたらうまくいった

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:localeConfig="@xml/locales_config"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Hello_world">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.DayNight">

            [snip]

        </activity>
    </application>

</manifest>