Chris
Chris

Reputation: 4368

Android dynamic language string

I've declared a string

 <string name="body_activity_welcome">Hello! <br />This is the welcome page. Only logged in users can see this page.</string>

But I want to personally greet the user

 <string name="body_activity_welcome">Hello {$username}! <br />This is the welcome page. Only logged in users can see this page.</string>

Is there a way to pass data to a language string when fetching it?

    TextView text = (TextView) findViewById(R.id.register_status_message);
    text.setText(R.string.body_activity_welcome); // somehow pass username to the string?

Upvotes: 0

Views: 72

Answers (1)

Marco Acierno
Marco Acierno

Reputation: 14847

You should use getString(), and replace {$username} with %1$s.

Where

%1: is the argument number

$s: is the type

The first argument of getString is the resource id, then you have a varargs which is all data you want to pass to the string.

getString(R.string.body_activity_welcome, userName);

TextView text = (TextView) findViewById(R.id.register_status_message);
text.setText(getString(R.string.body_activity_welcome, username)); //

Upvotes: 2

Related Questions