Reputation: 441
I have the following xml code:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Button" <!--Warning -->
android:textSize="45dp" <!--Warning -->
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvDisplay" />
In the xml code i found two warning first that dp
contains that which i got the waring to use sp
indeed. What is the reason it showing so?
Second warning and may be error is that i am using android:text="Press Button"
it tell me to use @string indeed. If i uses the same @string
is displayed in text which look awkward. What is the reason for it!
Upvotes: 0
Views: 2218
Reputation: 10887
Hardcoded String
value in View
is not recommeded by developer.android.com as making of Android Application compatible with different languages is twisted up to.
Referenced from
To add support for more languages, create additional values directories inside res/ that include a hyphen and the ISO country code at the end of the directory name. For example, values-es/ is the directory containing simple resourcess for the Locales with the language code "es". Android loads the appropriate resources according to the locale settings of the device at run time.
Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:
MyProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
Add the string values for each locale into the appropriate file.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.
For example, the following are some different string resource files for different languages.
English (default locale), /values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources>
Spanish, /values-es/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mi Aplicación</string>
<string name="hello_world">Hola Mundo!</string>
</resources>
Referring to your OP:
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="press">Press Button</string>
</resources>
This layout XML applies a string to a View:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/press"
android:textSize="45dp" <!--Warning -->
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvDisplay" />
This application code retrieves a string:
String string = getString(R.string.hello);
Use sp for setting size
as suggested by developer.android.com
sp : Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
XML file saved at res/values/dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="font_size">16sp</dimen>
</resources>
This application code retrieves a dimension
Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);
This layout XML applies dimensions to attributes:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Button" <!--Warning -->
android:textSize="@dimen/font_size"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvDisplay"
/>
Upvotes: 1
Reputation: 6711
It's better to use SP instead of DP.
It's recommend to use always string resources file, because if you need to change a single @String used in multiples xml files, you have to change only one time. Vice-versa, if you write your strings inside the xml layout files, if you need to change a string you need to search for the string, search for the xml file and then change as many occurrances you need.
In conclusion, it is not a good practice to hard code strings. You should specifies them to a string resource file and then reference them in your layout.This allows you to update every occurrence of a single word in all layouts at the same time by just editing your strings.xml file.
It is also necessary for supporting multiple languages definitions as a separate strings.xml One file for one language!
Upvotes: 0
Reputation: 6201
Android stander TextView size is use SP
and you are hardcode String that i give warning.
Please User your String.xml
in value folder and select this String then it do not give any error.
Thanks
Upvotes: 0
Reputation: 2300
Recommended dimension type for text is "sp" for scaled-pixels (example: 15sp)
from the developer.android
Upvotes: 0