Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one combine android resource strings into new strings?

Tags:

android

Android allows to create aliases of resource strings like:

<resources>
 <string name="foo">somestring</string>
 <string name="bar">@string/foo</string>
</resources>

by which the resource "bar" becomes an alias for the resource named "foo".

What I would for my app is a possibility to combine an existing resource prefix with different suffixes, i.e. to extend it like:

<resources>
 <string name="foo">foo</string>
 <string name="bar">@string/foo+bar</string> 
 <string name="else">@string/foo+else</string> 
</resources>

where the resource "bar" would yield the string "foobar". Its clear that '+' doesn't work here but is there some other option to achieve such a string concatenation, so that one could define a bunch of string resources that have a common prefix?

I realize of course that I could do such resource string concatenation at runtime but defining them statically in the resources would seem so much more elegant and simpler.

Michael

like image 304
mmo Avatar asked Aug 31 '10 22:08

mmo


2 Answers

I know it's late, but anyways now you can do so by using this library I've created: https://github.com/LikeTheSalad/android-string-reference

It will concat all of the strings you want to at build time and will generate new strings that you can use anywhere in your project as with any other manually added string.

For your case, you'd have to define your strings like so:

<resources>
 <string name="foo">foo</string>
 <string name="template_bar">${foo} bar</string> 
 <string name="template_else">${foo} else</string> 
</resources>

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

<resources>
 <string name="bar">foo bar</string> 
 <string name="else">foo else</string> 
</resources>

These auto generated strings will keep themselves updated for any changes that you make to your templates and values. They also work with localized and flavor strings. More info on the repo's page.

like image 125
César Muñoz Avatar answered Sep 22 '22 05:09

César Muñoz


No, it's not possible. You can just use <string name="bar">This is my %s</string> and then use String.format() in your app to fill in the variable with for example

getResources().getString(R.string.foo);
like image 39
Mathias Conradt Avatar answered Sep 20 '22 05:09

Mathias Conradt