0

I'm working on a project where I've defined several strings to use on my project.

I want to use a string to be displayed as my subtitle of the page on the toolbar. The reason I'm using strings is because I want my app to be translation supported.

Here is how I use subtitles on the toolbar of my activity:

android.support.v7.app.ActionBar ab = getSupportActionBar();
    ab.setTitle("Title");
    ab.setSubtitle("Subtitle");

I want to use a string on java (like @string/helloworld in xml) but I don't know how can I do that.

Can anyone help me?

Arda Çebi
  • 1,705
  • 4
  • 15
  • 27

3 Answers3

2

In your "res" directory, there might be "strings.xml" file. (If you didn't remove it). Add string tags like bellow code snippets.

<string name="title">Title Message</string>
<string name="sub_title">Sub Title Message</string>

And in your java file.

String mStringTitle = getString(R.string.title);
String mStringSubTitle = getString(R.string.sub_title);

You can also use these string resources in your layout XML like follows.

<TextView
    android:text="@string/title" />

For more information, please refer to the bellow URLs.

What is the string resource in Android? android_string_resources

How to support multiple locales? support_different_language // different-locales

Joonsung Kim
  • 246
  • 1
  • 3
  • 15
  • Already knew how to use strings on xml and creating strings, just didn't know using them in Java. But the answer you submitted can help other users so this one's the most useful one. Thank you. @JoonSungKim – Arda Çebi Jan 31 '18 at 21:52
1

In this case, use R.string.helloworld, because these methods require a resource ID.

YMY
  • 678
  • 9
  • 17
0

This is the code -

R.string.message_failure
    

Implementation of the above code -

snackbar = Snackbar.make(constraintLayout,R.string.message_failure, Snackbar.LENGTH_SHORT)
    
    
//String_file.xml code - 
    
<string name="message_failure">Failure</string>