Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is concatenation of resource strings/string concatenation, possible in layout file?

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"
like image 422
Nitish Avatar asked Aug 21 '14 05:08

Nitish


People also ask

What are the 2 methods used for string concatenation?

There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.

Which of them is an example of string concatenation?

In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball".

Which is the efficient way of concatenating the string in Java?

StringBuilder is the winner and the fastest way to concatenate Strings. StringBuffer is a close second, because of the synchronized method, and the rest of them are just 1000 times slower than them.


2 Answers

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?

like image 162
Andrew Avatar answered Sep 18 '22 12:09

Andrew


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!

like image 34
Aldo Borrero Avatar answered Sep 21 '22 12:09

Aldo Borrero