what is the use of string.xml file in android?
is it like defining global strings if we have multiple .java files in android application? correct me if i am wrong

Dani AI

Generated

As asked and as pointed out, the strings.xml file is the app's canonical place for user-visible text. It lives under res/values/strings.xml and stores named string resources that the build system compiles into the R class. These resources are convenient for maintenance and translation, but they are not Java "global variables" in the normal sense — they are resource IDs that must be resolved via a Context at runtime.

Example patterns:

<!-- res/values/strings.xml -->
<resources>
  <string name="app_name">MyApp</string>
  <string name="welcome">Welcome, %1$s!</string>
  <plurals name="unread_count">
    <item quantity="one">%d message</item>
    <item quantity="other">%d messages</item>
  </plurals>
</resources>

Usage in XML and code:

android:text="@string/app_name"

String s = getString(R.string.welcome, userName);
textView.setText(R.string.unread_count); // setText has an overload that accepts a resource id

Practical tips and cautions: prefer numbered placeholders (%1$s) so translators can reorder text; use <plurals> for counts; mark non-translatable UI-only strings with translatable="false"; do not store secrets or runtime data in resource files. If a string change does not appear, perform a Clean/Rebuild or check resource name rules (lowercase, underscores). For official details and localization workflow, see the Android string resources guide: Android string resources.

  • Reduce duplication and therefore many places to change if same string declared in multiple places
  • Easy way of managing and sharing resource that has to be translated if you need internatioanlization for application
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.