What Is Strings.xml???
In this tutorial you will learn about the followings-
- What is Strings.xml and Why do we need it?
- Where it is located in your project.?
- How to modify an existing String Resource.?
- How to add a new String Resource.?
What is Strings.xml and Why do we need it?
String.xml is a single location for various strings your application needs. here every string has a unique id, this id you use in your code to use that string. There are two ways to handle a string resources,
- Resources Tab
- strings.xml Tab
Why do we need it?
Instead of providing the string id we can just provide the actual string.
android:text=” i m a hard core text here” instead of android:text=”@string/hello_world”
then why do we do this??, you can think of a scenario when you have to write a same string at more than 10-20 places and if you don’t use the string id and you want to change the string value, you will have to change it at every place but if you are using string id, you need to change it at only one place.
Where it is located in your project.?
Your Project-> res -> values -> string.xml
How to modify an existing String Resource?
We can modify the existing string resource either in Resources tab or in strings.xml tab.
First we will modify using strings.xml tab
We will change android:text=“@string/hello_world” .
Hello-> res -> values -> string.xml. open it.
Click on the string.xml at the left bottom.
in last two figures check the “hello_world”, it’s an String id and its value is “Hello World” so if we change it we will change the text appears on the screen.
Change it to “I am Sumit Jain, saying Hello to the android world”
You can modify it from resources tab as well, just select the string resource you want to modify and change its attributes, Name and Value at the right side.
How to add a new String Resource.?
Open Strings.xml from project explorer and select the Resources tab in it if already not selected, and click Add button .
Select the String from the new window and click ok.
Now you will see the new string resource added under Resources Element List. Select it, you will see the Attributes for String on the right side. Enter the Name and Value in it.
That’s it your string resource has been added.
Happy Androiding 🙂
If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.