Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Making App name dynamic in strings.xml in Android

I have an Android app where the app name is mentioned in different places of the app. Currently in strings.xml, app_name is "Wonder11" which is general that everyone know. Next, in the registration page, I will have the app name as "Register now and Join Wonder11". And again in the same page, I will have "By joining, you accept Wonder11’s Terms and Conditions.". Also, I will have the same in welcome message. So now the strings.xml file is like this:

<resources>
    <string name="app_name">Wonder11</string>
    <string name="registration_header_message">Register now and Join Wonder11</string>
    <string name="welcome_message">Welcome to Wonder11!</string>
    <string name="joining_message">By joining, you accept Wonder11\'s Terms and Conditions.</string>
</resources>

Instead of having the same app name in 4 different places in strings.xml, is there a way to dynamically use the app_name in the rest of the 3 places? I have searched a lot on Google but I din’t find any solution. The intention is if I am changing the app name in future, I would like to change in one place which is app_name only. So that it does the rest in other places when the value is used dynamically rather than changing the app name in so many places. This 3 more place is just the beginning. When the app creation goes on, I might have another 10-15 places where the app name needs to be used in the whole app.

Can someone please help?
Thanks in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

<resources>
    <string name="app_name">Wonder11</string>
    <string name="registration_header_message">Register now and Join %s</string>
    <string name="welcome_message">Welcome to %s!</string>
    <string name="joining_message">By joining, you accept %s\'s Terms and Conditions.</string>
</resources>

Code :

Resources res = getResources();
String registration = String.format(res.getString(R.string.registration_header_message),res.getString(R.string.app_name));

You can format the string like this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading