Define strings

suggest change

Strings are typically stored in the resource file strings.xml. They are defined using a <string> XML element.

The purpose of strings.xml is to allow internationalisation. You can define a strings.xml for each language iso code. Thus when the system looks for the string ‘app_name’ it first checks the xml file corresponding to the current language, and if it is not found, looks for the entry in the default strings.xml file. This means you can choose to only localise some of your strings while not others.

/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Hello World App</string>
    <string name="hello_world">Hello World!</string>
</resources>

Once a string is defined in an XML resource file, it can be used by other parts of the app.

An app’s XML project files can use a <string> element by referring to @string/string_name. For example, an app’s manifest (/manifests/AndroidManifest.xml) file includes the following line by default in Android Studio:

android:label="@string/app_name"

This tells android to look for a <string> resource called “app_name” to use as the name for the app when it is installed or displayed in a launcher.

Another time you would use a <string> resource from an XML file in android would be in a layout file. For example, the following represents a TextView which displays the hello_world string we defined earlier:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"/>

You can also access <string> resources from the java portion of your app. To recall our same hello_world string from above within an Activity class, use:

String helloWorld = getString(R.string.hello_world);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Resources:
* Define strings

Table Of Contents
2 Gradle
5 Intent
8 Resources
17 Service
19 WebView
31 SQLite
35 Glide
37 Dialog
38 ACRA
44 Handler
53 Toast
63 Menu
65 Picasso
70 Volley
71 Widgets
78 Realm
90 Spinner
95 OkHttp
108 TextView
109 ListView
111 Loader
118 Xposed
119 Security
121 ImageView
123 Doze Mode
130 Drawables
131 Colors
134 Fresco
139 AdMob
145 Keyboard
146 Button
150 EditText
155 Vk SDK
163 ExoPlayer
169 XMPP
175 OpenCV
177 Threads
184 ORMLite
186 TabLayout
190 LruCache
192 Zip files
194 Fastlane
199 FileIO
202 Moshi
210 VideoView
216 Paint
218 ProGuard
226 CleverTap
228 ADB shell
229 Ping ICMP
230 AIDL
234 Context
240 JCodec
242 Okio
249 FuseView
254 Looper
261 Fastjson
263 Jackson
267 Smartcard