0

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?

1 Answer 1

2

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)); //
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.