Hi, I was running my app in android studio and they gave me several errors in that file, especially in the names of the variables and I would like someone to solve my doubt, thanks.
Hi, I was running my app in android studio and they gave me several errors in that file, especially in the names of the variables and I would like someone to solve my doubt, thanks.
Serves to store strings that can be used in different parts of the application, either in Java or elsewhere, as in XML files.
Thus, the value of the chain is defined in a single site, and is referenced by its attribute name
.
The advantage is that the code of the app is more flexible. Imagine a chain that repeats hundreds of times in the app and then you have to change it in each place. In addition, having the strings in a resource file facilitates the translation of the app into other languages. Such a translation would be an almost impossible task if you dedicate yourself to handwriting the chains everywhere in the app.
If you have the string values only in the String resources, you modify it there and the string is automatically updated wherever it is used.
The string resource is referenced as follows:
In Java: R.string.string_name
In XML: @string/string_name
Having resources such as chains and others (dimensions, styles, ...) is part of a set of strategies in the framework of Android programming.
In Android Studio for example, when you have a handwritten string anywhere, it tells you a warning that you must convert that resource ... even AS searches if that resource exists in the resource file and with a single click gives you the possibility to update it.
For example here:
The message that appears in the yellow box gives two more than compelling reasons to apply the use of chain resources:
The text attributes of direct coding in the files of Design are incorrect for several reasons:
When creating configuration variations (for example, for landscape or portrait), you must repeat the actual text (and keep it updated to the make changes)
The application can not be translated into other languages by simply adding new translations for existing string resources.
[In Android Studio and Eclipse] there are quick fixes to extract automatically this encoded string in a resource search.
That's why AS recommends changing the hardcoded string, for a string resource, which also exists already in this case. The image shows the change made from the value Liturgia+
to the resource reference app_name
:
In the string resource file ( strings.xml
), the resource looks like this:
That way, if you need to update it, it would only do so in strings.xml
. Consequently, in any layout that I use: android:text="@string/app_name"
or in any Java code that uses: R.string.app_name
the current value will be displayed.
You can find more details on the meaning of this practice in the section General information of resources , from Android:
You should always outsource resources as images and strings of your application code so you can maintain them independent . Outsourcing resources also allows you provide alternative resources that support configurations specific devices, such as languages or screen sizes different, which is increasingly important, as more devices with Android technology are available with configurations different In order to provide compatibility with configurations different, you must organize the resources of the directory
res/
of the project, for which you must use several subdirectories that group resources by type and configuration.
There are different types of String resources, which are explained on the page Strings resources from the Android documentation.
They are not errors, they are "warnning" , that is, a warning.
The strings.xml
file is used to store all the character string constants that are needed in a program (for example the tags of Button
objects, the fixed texts of the controls TextView
and all controls that show a fixed text on the device)
The fundamental idea is to have all the messages that our application in a file (strings.xml)
For example:
As the image shows, the first String
is for the name of your project. and it is called by the name app_name
. You can create various String
as simple or Arrays among others.
Ash's answer is very good and I would give it as correct, to add to it the way it is done:
you create a string tag such that:
<string></string>
Now you give it a name so it looks like this:
<string name="colorFavorito"></string>
Finally you put the value of the resource so it looks like this:
<string name="colorFavorito">Rojo</string>
Now suppose your layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
To get what we want we finally add the attribute 'text' to the textview with the reference to the resource so it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/colorFavorito"/>
</LinearLayout>
That is @string because that's the name of the label and then the name you gave it: (in our case 'colorFavorito') so that what is finally shown is what comes between the label (in our case 'red ')