cheziHoyzer
cheziHoyzer

Reputation: 5041

Android - Extracting string from .java code to string.xml

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.

Upvotes: 0

Views: 762

Answers (2)

Anand Singh
Anand Singh

Reputation: 5692

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)); 

Upvotes: 4

Kinnar Vasa
Kinnar Vasa

Reputation: 407

You can also define java string in string.xml

and you can use it like:

getString(R.string.string_name);

Upvotes: 2

Related Questions