Dragneel
Dragneel

Reputation: 181

Why is it a good habit to use strings.xml for TextViews?

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

Answers (4)

Ahmed Bettaieb
Ahmed Bettaieb

Reputation: 74

There is a lot of reasons when you use string.xml...

  1. Avoid repetition: In order to have a well structured app you need to not reuse strings for different screens,imagine that you have 5 screens each one needs to show up a toast "Loading..." and later you decide to change it to "Please wait..." so you need to iterate all the 5 toasts making changes but if you string.xml then it will be change once use multiple :D ! <string name="loading_toast">Please wait...</string>
  2. Localization:You never know which language your application may support,so you will use string.xml in order to store complex words like the Russian language.Imagine you have tens of strings in your java file like this "Привет мир" which means "Hello World!" then you will be confused sometimes (as my experience did :D)
  3. 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

Fazal Hussain
Fazal Hussain

Reputation: 1127

Use Butter knife library

@BindString(R.string.course_days)
String courseDays;

In onCreate

Butterknife.bind(this);

Upvotes: 0

Levi Moreira
Levi Moreira

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

Umar Ahmed
Umar Ahmed

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

Related Questions