Stanly El
Stanly El

Reputation: 63

Concatenating strings in Textview (in the layout)

I had a ressource string : "@string/audit" for Audit, in my XML Text view I tried to add a prefix "-" before my string Audit to get something liket that : - Audit

  <TextView
                android:id="@+id/openActionMenu"
                android:background="@color/white"
                android:layout_width="match_parent"
                android:layout_marginLeft="35dp"
                android:padding="2dp"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="@color/TealDark"
                android:textSize="14sp"
                android:layout_gravity="center"
                android:text="- @string/audit"/>  // <- this line

how to acheive that?

P.S.: I don't need a programmatic way, I look for in XML

Upvotes: 1

Views: 357

Answers (2)

C&#233;sar Mu&#241;oz
C&#233;sar Mu&#241;oz

Reputation: 645

If you don't want to duplicate strings nor write any java or kotlin code to resolve placeholders at run time, you could use this library I've created: https://github.com/LikeTheSalad/android-stem

Which will concat as many strings as you like at build time, and will generate new strings that you can use anywhere as with any other manually added string.

For your case, you could define your strings like so:

<string name="audit">Audit</string>
<string name="dash_audit">- ${audit}</string>

And after building your project, you'll get:

<!-- Auto generated during compilation -->
<string name="dash_audit">- Audit</string>

You'll still have to use a different string name (dash_audit) in your layouts though 😅but at least you keep the "Audit" value in a single place, so whenever you decide to change it, you just do it once and then the tool will update the generated strings with the new values. More info on the repo's page.

Upvotes: 0

em_
em_

Reputation: 2269

Short answer is, you can't in xml

So instead, in your strings.xml

<string name="dash_audit">- Audit</string>

Upvotes: 3

Related Questions