I have resource strings :
<string name="address">Address</string>
<string name="city">City</string>
<string name="country">Country</string>
<string name="pincode">Pincode</string>
In my application, at few places I am using these strings alone and at few places I am succeeding them by a colon.
I don't what to create another four resource strings :
<string name="address_with_colon">Address: </string>
<string name="city_with_colon">City: </string>
<string name="country_with_colon">Country: </string>
<string name="pincode_with_colon">Pincode: </string>
Now to achieve this, I have to concatenate my resource strings with colon. I know this is very easy though java code which I can write in my activity class. But what I want is to do the concatenation in my layout file.
Question : Is string concatenation possible in layout file?
This is where I have to perform the concatenation:
android:text="concatenation_if_possible"
Using XML entities it's possible to use the same string multiple places within an XML file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
<!ENTITY appname "MrQuery">
<!ENTITY author "Oded">
]>
<resources>
<string name="app_name">&appname;</string>
<string name="description">The &appname; app was created by &author;</string>
</resources>
I used this answer: dynamic String using String.xml?
Question : Is string concatenation possible in layout file?
Nope as far as I know.
One solution is to use what #DIVA has answered before.
Another possible solution is to create a custom view that extends TextView (or the view you want to achieve this) and create a custom attribute custom:concatenate which receives a string reference and perform the concatenation automatically. IMHO I think this is the most clean approach.
In code will look as this:
<com.whatever.ConcatenateTextView
android:text="#string/whatever"
custom:concatenate="#string/second_string"/>
Or… you can use the power of Drawables creating a custom TextDrawable (which is explained very well by #Devunwired in this post and the concrete implementation of it in Github).
Copying what #Devunwired has said in his post about it:
With this class, text can now be part of the Drawable world, meaning it can not only be set alone in places where you would normally put an image, it can also be placed together with other Drawables in containers like StateListDrawable or animated with the likes of TransitionDrawable and ClipDrawable. In many cases, we can use this to do a job that would otherwise require multiple views or compound controls just to achieve a given visual effect; thus it can reduce overhead in your view hierarchy.
This combined with your custom TextView as I explained before (or whatever view you want to use) gives you a very powerful option. Again copying the example that #Devunwired wrote in his post:
ImageView mImageOne;
TextDrawable d = new TextDrawable(this);
d.setText("SAMPLE TEXT\nLINE TWO");
d.setTextAlign(Layout.Alignment.ALIGN_CENTER);
mImageOne.setImageDrawable(d);
If you need more help please let me know in the comments and I'll gladly update the answer!
You can so something like this :
<string name="meatShootingMessage">You shot %1$d pounds of meat!</string>
String strMeatMsg = String.format(strMeatFormat, ":");
textview.setText(strMeatMsg);
No, you cannot concatenate several string resources into a single string when directly referencing those strings from a layout file.
XML layout files are simply a template of instructions for Android to build a user interface, and you should consider them as a cleaner and more organized way to generate your UI than using a Java class that manually creates and positions views in a layout. That being said, there are limitations to what you can do with a layout file, and one of them is being able to reference a single string resource from every view, meaning that you can't do anything more complex than that, including concatenating several strings into one.
You can do so using this plugin I've created: https://github.com/LikeTheSalad/android-stem It will concat all of the strings you'd like to and will generate at build time a final XML string that you can reference anywhere as with any other manually string you've added.
For your case, you can do the following:
<string name="address">Address</string>
<string name="city">City</string>
<string name="country">Country</string>
<string name="pincode">Pincode</string>
<string name="address_with_colon">${address}: </string>
<string name="city_with_colon">${city}: </string>
<string name="country_with_colon">${country}: </string>
<string name="pincode_with_colon">${pincode}: </string>
And then the tool will generate:
<!-- Auto generated during compilation -->
<string name="address_with_colon">Address: </string>
<string name="city_with_colon">City: </string>
<string name="country_with_colon">Country: </string>
<string name="pincode_with_colon">Pincode: </string>
And anytime you decide to change either your template or values strings, the plugin will keep the generated strings updated. More info on the repo's page.
Related
Is there a way to set the value of an xml string tag value to include that of another string tag value. The idea is like this:
<string name="tag1">"this is"</string>
<string name="tag2"><tag1 + " what I mean"</string>
Is this possible and if so how do I do it?
Is this possible and if so how do I do it?
Arithmetic, concatenation operations are not possible in android resource files.
You can concatenate strings 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="tag1">this is</string>
<string name="tag2">${tag1} what I mean</string>
And then after building your project, you'll get:
<!-- Auto generated during compilation -->
<string name="tag2">this is what I mean</string>
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>
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));
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>
Is there a way in Android to translate strings with an own translation file at runtime?
e.g.
english file:
hello world : Hello world.
german file:
hello world: Hallo Welt.
Like in Webframeworks (Symfony, Rails, ...)
I can think of two ways to do so. One is to specify the Android language/region code and use different res/values/strings.xml to translate the string values. But this will affect the entire app. Here is the reference on supporting different languages
Another way to do so is to create localized resource file like this for server response translation.
So the strings.xml would be like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="error_msg_ge">Fehlermeldung</string>
<string name="error_msg_en">Error Message</string>
</resources>
Below is just a pseudo code since you didn't post any code.
if(!isSuccess) {
TextView textView = new TextView(this);
textView.setText(R.string.error_msg_ge)
}
If we really want to be more precise, then we can specify more error messages in the resource file and swap to use based on the error message returned from the server.
So the XML could be like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="error_ge_timeout">auszeit</string>
<string name="error_ge_noresult">kein Ergebnis</string>
<string name="error_en_timeout">timeout</string>
<string name="error_en_noresult">no result</string>
</resources>
Then
if(!isSuccess) {
TextView textView = new TextView(this);
if(errMsg.equals(R.string.error_en_timeout)) {
textView.setText(R.string.error_ge_timeout);
} //...and so on
}
Lastly but not the least, you can also utilize third party API to do the translation. But since you are translating error message received from the server, I am not sure if that is necessary since it is not a conversational purpose so it could be an overkill. In any case, there are many ways to achieve it.
I have updated some string values in strings.xml and my application now shows not the new text but something like #234545201. I have cleaned the projected and rebuilded, there are no import android.R anywhere, just R related to my package. What went wrong?
To obtain a string from your strings.xml file, you can do a few things.
If you need it as a String object, you can use getString(R.string.string_id) to fetch the string, given an ID.
If you're trying to set the text of, say, a TextView, you can actually simply use setText(R.string.string_id) and the OS will obtain the correct string for you.
In other words, the TextView class has a method called setText(int resid), and that's also the reason why you can't write something like the following:
TextView.setText(12345680);
Are you trying to read it directly as R.string.my_string_resource?
Try passing it to getString() as getString(R.string.my_string_resource).
You can put your stings.xml file in the follwing format.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Yamba</string>
<string name="titleYamba">Yamba</string>
<string name="titleStatus">Status Update</string>
<string name="hintText">Please enter your 140-character status</string>
<string name="buttonUpdate">Update</string>
</resources>
and use the name as reference of your textbox ids like.
android:text="#string/app_name"
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
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);
I know it's late, but anyways now you can do so by using this library I've created: https://github.com/LikeTheSalad/android-stem
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="bar">${foo} bar</string>
<string name="else">${foo} else</string>
</resources>
And then, after building your project, you'll get:
<!-- Auto generated during compilation -->
<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.