KMP Text from Common Resource String.xml in Compose Multiplatform

Today’s tutorial teaches how to use KMP Text from the Common Resource String.xml file in Kotlin Compose Multiplatform. This is useful when we add multiple text strings to our app because all the strings are maintained using a single file. We can reuse Text strings when calling from a String.xml file. It also plays a major role in integrating localization in our Android and iOS applications.

KMP Text from Common Resource String.xml in Compose Multiplatform

KMP Text from Common Resource String.xml in Kotlin Compose Multiplatform

All the native Android Java & Kotlin developers are familiar with String.xml files because we all have used them. It would be new to you if you came from another development arena. So, the primary purpose of using String.xml is to put all the Text Strings within a single XML resource file, which we can call from there.

Setup KMP project for String.xml file :

1. String.xml is not in our KMP Compose multiplatform project by default. We must create this file manually.

2. To create a String.xml file, go to your KMP project -> composeApp -> src -> composeResource. Create a folder named values.

3. In the values folder, create a file string.xml file.

KMP Text from Common Resource String.xml in Kotlin Compose Multiplatform

4. Paste the code below into the string.xml file. For learning purposes, I have created only two String keys. You can create or modify these keys and their Strings.

5. Now, you must rebuild your project. To rebuild the KMP project, click Build -> Rebuild Project.

Rebuild Android Studio project.

6. you can access these Strings in your KMP project after successfully rebuilding the project. To do so, use the stringResource(Res.string.your_key) format. See the code example below.

7. Accessing another String key from the String.xml file in Text.

Complete source code for App.kt file:

Screenshot in Android device:

KMP Text from Common Resource String.xml in Compose Multiplatform

Screenshot in iOS device:

iOS String.xml file Text

Thanks for reading our article. If you have any issues integrating this functionality, please feel free to comment. I will surely reply to you and resolve your issue.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *