String.XML In Android

Introduction

String.xml file contains all the strings which will be used frequently in Android project. String.xml file present in the values folder which is sub folder of res folder in project structure.In Android Studio, we have many Views such as TextView,Button,EditText,CheckBox,RadioButton etc. These views have a common attribute named text. we can assign  value to this attribute directly or via string.xml. Please follow below given path to go on string.xml file.

  • Double click on  app folder
  • Double click on res folder
  • Double click on values folder
  • Double click on strings.xml

Why it is Required?

In large applications, we use some text many times in our project so rather than define that particular text many times. it is best to store in string.xml file and call it by its string name in project.

For example: Suppose we have a text:- “Hey How are You” 

Our requirement is to write this text 100+ times in any location of the project. If we write this text 100+ times, it require extra efforts. It is best to store this text in string.xml once and call string wherever required. This will save a lot of efforts and less line of code.

String file also contain string array. we can use string array further in andorid project .For example if we have a name list then we can define this list using string array in string.xml .

How can we use it?

First, look at the below picture and see that “Hey how are you” string used 3 times.

Step 1

Go to String.xml file (app->res->values->strings.xml)

Step 2

Now define a String in string.xml.

โ€‹<string name="greeting">Hey How are you</string>

   

Step 3

Now Go to layout XML file and added this code .Here we call this string by using @string/greeting.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/greeting"
        android:textSize="30dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/greeting"
        android:textSize="30dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/greeting"
        android:textSize="30dp"/>

</LinearLayout>

Output

 


Here we took a linear layout with orientation vertical and add three textview inside the linear layout. We did not write any text directly in textview however first we added the required text into string.xml file and call this string to show text in textview field. Syntax for calling the string will be; @string/greeting.

How can we use String Array ?

Step1

Go to String.xml and add the below xml code inside the <resource></resource> tag.

<string-array name="Person">
    <item>Harshit</item>
    <item>Abhishek</item>
    <item>Ram</item>
    <item>Shayam</item>
</string-array>

 Here we have a string array and the name of this string array is Person.

Step 2

We can use this string-array in MainActivity.java with the help of following code

String str[] = getResources().getStringArray(R.array.Person);

Here we created a string array with name person in string.xml. This string array contains set of text based on our requirements. Now we can call this string array in MainActivity.java.

Conclusion

In this article we have seen, how to define string & string array in string.xml and how to call these string in the android project.

Thanks for reading and hope you like it. If you have any suggestion/query on article. Please share your thoughts.