Issue
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.
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.
Answered By - Kishan Maurya
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.