Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android strings xml similar texts

In my Android app I'm using strings.xml for all texts. I have many situations where I use almost the same string,

e.g. "Name" and "Name:" - translation is the same only additional colon is difference.

Is there any other way to have these two string except creating two string items like this:

<string name="name">Name</string>
<string name="name2">Name:</string>
like image 697
user3626048 Avatar asked Mar 14 '26 01:03

user3626048


2 Answers

There is no way you can concatenate strings in the strings.xml file.

All you can do is specify the format,

<string name="name">Name</string>
<string name="string_with_colon">%s:</string>

Then pass the name programatically,

String.format(getString(R.string.string_with_colon), getString(R.string.name));
like image 198
pratZ Avatar answered Mar 16 '26 16:03

pratZ


Yes, you can do so without writing any Java/Kotlin code, only XML by using this small library I created which does so at buildtime: https://github.com/LikeTheSalad/android-stem

Usage

Based on your example, you'd have to set your strings like this:

<string name="name">Name</string>
<string name="name2">${name}:</string>

And then after building your project, you'll get:

<!-- Auto generated during compilation -->
<string name="name2">Name:</string>
like image 41
César Muñoz Avatar answered Mar 16 '26 16:03

César Muñoz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!