6

I am displaying a Error message in a Toast with strings.xml.

Like this mErrorMsgId=R.string.username_or_password_incorrectfull;

But now i need to concatenate the message with R.string.add_selfhosted_blog to avoid translation inconsistencies.

Read some similar questions but can't figure it out.

EDIT:

I want to concatenate nux_add_selfhosted_blog after the word tap in the username_or_password_incorrectfull string ...

<string name="username_or_password_incorrectfull">The username or password you entered is incorrect
 \- If you\'re a self hosted user, don\'t forget to tap **Add Self Hosted Site** and fill the URL field</string> 

<string name="nux_add_selfhosted_blog">Add self-hosted site</string>

How can i acheive this ??

Arulnadhan
  • 923
  • 4
  • 17
  • 46

8 Answers8

5

You cannout concatenate R.string.username_or_password_incorrectfull with R.string.add_selfhosted_blog directly as they are not String instances as such but rather the resource id to the actual String in the strings.xml. You can get the 2 strings and then concatenate them normally.

Something like this:

String string1 = getResources().getString(R.string.username_or_password_incorrectfull);
String string2 = getResources().getString(R.string.add_selfhosted_blog);

String combined = string1 + string2;
Rahul
  • 44,383
  • 11
  • 84
  • 103
1

Second parameter of method makeText accepts stringResId or just String

For example:

String error = getResources().getString(R.string.username_or_password_incorrectful);
String error2 = getResources().getString(R.string.add_selfhosted_blog);
String conctString = error + " " + error2;
Toast.makeText(context, conctString, duration).show();
kstachniuk
  • 358
  • 1
  • 4
1

you can use MessageFormat.

set username_or_password_incorrectfull as the format string

... forget to tap {0} and fill the URL...

then use MessageFormat.format(username_or_password_incorrectfull, nux_add_selfhosted_blog)

dongwq
  • 99
  • 1
  • 3
0

If you want to simply concate those two String from strings.xml resource then you can achieve by this...

String errorMessage = getString(R.string.username_or_password_incorrectfull) + getString(R.string.add_selfhosted_blog);

Then show the concated error message in the `Toast

Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show();
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
0

You can insert a string in Middle of another string in following way,

String mErrorMsgId = getResources().getString(R.string.username_or_password_incorrectfull); // +  getResources().getString(R.string.nux_add_selfhosted_blog);

String firstPart = mErrorMsgId.substring( 0, mErrorMsgId.indexOf( "tap") + 4 );
String selfhosted_blog = getResources().getString(R.string.nux_add_selfhosted_blog) + " ";
String thirdPart = mErrorMsgId.substring( mErrorMsgId.indexOf( "tap") + 4 );
mErrorMsgId = firstPart + selfhosted_blog + thirdPart;
Lucifer
  • 29,392
  • 25
  • 90
  • 143
0

Just retrieve string from both id's and concate with + as ususal and put resulted string variable in Toast.

String messageToUser = getResources().getString(R.string.username_or_password_incorrectfull) +  getResources().getString(R.string.add_selfhosted_blog);
Toast.makeText(context, messageToUser, Toast.LENGTH_SHORT).show();
Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
0

You can do so by using this library I've created: https://github.com/LikeTheSalad/android-stem it will concatenate all of you strings you want to based on a template string you define, and then it generates a final XML string at build time. So for your case, you can define your strings like so:

<string name="nux_add_selfhosted_blog">Add self-hosted site</string>
<string name="username_or_password_incorrectfull">The username or password you entered is incorrect
 \- If you\'re a self hosted user, don\'t forget to tap ${nux_add_selfhosted_blog} and fill the URL field</string>

And then after building your project, the following string will be generated:

<!-- Auto generated during compilation -->
<string name="username_or_password_incorrectfull">The username or password you entered is incorrect
     \- If you\'re a self hosted user, don\'t forget to tap Add self-hosted site and fill the URL field</string>

The tool will keep this auto-generated string updated for any changes you make to the template and or the values. More info on the repo's page.

César Muñoz
  • 535
  • 1
  • 6
  • 10
0

You can simply add/concatenate string as you do in C#/Java,

Here Address1 and Address2 are EditText(TextBox)

String finalString = Address1.getText().toString() + " "+ Address2.getText().toString();
Sheriff
  • 738
  • 10
  • 20