Reputation: 181
I have
aTextView.setText("Days\n" + getClassDays());
However, I hear utilizing strings.xml is the proper way of doing it. But if so, here's how I found the best way of doing it
strings.xml
<string name="course_days"><b>Days\n</b>%1$s</string>
MyActivity
aTextView.setText(String.format(_context.getString(R.string.course_days), getClassDays()));
As you can see, it's long and ugly. Am I missing a much simpler solution or something?
Upvotes: 1
Views: 122
Reputation: 74
There is a lot of reasons when you use string.xml...
<string name="loading_toast">Please wait...</string>
Separation:If you are working on big projects with thousands lines of code you might be writing some wrong texts,and your client asks to correct some of them,if you are using string.xml then you can specify a prefix of each screen and it will make it easy in correction and refactoring process here is an example :
<!--Log in screen start -->
<string name="login_screen_welcome_message">Welcome</string>
<string name="login_screen_log_in_button">Log In</string>
<!--Log in screen end -->
<!--Home screen start-->
<string name="home_screen_greeting_message">Hi there !</string>
<--Home screen end-->
Happy Coding
Upvotes: 3
Reputation: 1127
Use Butter knife library
@BindString(R.string.course_days)
String courseDays;
In onCreate
Butterknife.bind(this);
Upvotes: 0
Reputation: 11995
It makes your life easier when you need to localize your app. You'll need only to change the strings.xml file and all your app will be in other language. Also it's a more organized way to keep your strings, if you need to change one of them you'd go to one place only. For example if you have the same string in multiple activities and you want to change it, you change once in strings.xml ad that will be reflected throughout your activities. More info here.
Upvotes: 2
Reputation: 191
Whats the point of adding the bold tags in your string resources?
Just <string name="course_days">Days\n %d</string>
(%d
for int) in your string.xml and aTextView.setText(_context.getString(R.string.course_days), getClassDays()));
will work fine.
Note that if your setting the value of your textView in an activity or fragment then it will be just aTextView.setText(getString(R.string.course_days), getClassDays()));
Upvotes: 0