0

I know it's good practice to use strings.xml file for all your hard coded string, especially if you're developer of a multi language app.
It's all good with layout.xml files, you can easily use like this: android:text="@string/Login_text".
But what about .java files?
For example, what it's the best practice to get the strings (from separate file like strings.xml) from this code:

progress = new ProgressDialog(MainActivity.this);
    progress.setTitle("Some Title In Hebrew Language");
    progress.setMessage("Some Message In Hebrew Language");

I'm using Android-Studio.

3
  • strings (to separate file) ?? Commented Jul 13, 2015 at 11:02
  • You're right I edited it Commented Jul 13, 2015 at 11:09
  • you want those strings to be put in strings.xml?? is that what you are looking for. if you want strings from strings.xml the below answers will work Commented Jul 13, 2015 at 11:09

2 Answers 2

4
android:text="@string/Login_text"

and

getResources().getString(R.string.Login_text)

both are same. One is XML approach and other is Class file approach.

So write your code like this:

progress = new ProgressDialog(MainActivity.this);
    progress.setTitle(getResources().getString(R.string.sometext1)); 
    progress.setMessage(getResources().getString(R.string.sometext2)); 
Sign up to request clarification or add additional context in comments.

Comments

2

You can also define java string in string.xml

and you can use it like:

getString(R.string.string_name);

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.