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!
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"
/>
Recommended dimension type for text is "sp" for scaled-pixels (example: 15sp)
from the developer.android
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
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!
Related
I want to use a different string resource on small screens but Android always uses the default string. Am I doing it wrong?
/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue</string>
</resources>
/res/values-large/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue to Next Page</string>
</resources>
#string/continue always resolves to "Continue", even on large screens. It does, however use the dimensions in /res/values-large/dimens.xml so I'm sure I'm using the right resource qualifier. I also tried /res/values-w550dp/strings.xml and it didn't work either.
Am I doing something wrong or is this a limitation of Android?
values-large is only available for dimens and styles, but not for string resources.
one could possibly add custom string-arrays and then look them up accordingly.
see Android Resource Types.
In android xml:ish
Is there any way to change a visibility attribute based on the layout size/orientation in the xml directly?
I have a button in a fragment that should be visible for small screens sizes only. On larger sizes, let's say layout-large, I want it to be hidden.
Sure, I can write code for this without any problem but for academic reasons I would like to know it it's possible to do something like this.
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="<magic expression here>" />
Thanks
// Johan
This answer is based off the explanation provided here by Flávio Faria.
The visible, gone, etc can be values mapped to their corresponding enum values in a string resource - which means you can create a visibilty.xml with string resources for each layout you want, and Android will automatically resolve the one you want for each screen type.
I'd recommend the following:
/res/values/visibilty.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Enum values pulled from Android source -->
<string name="visibility_visible">0</string>
<string name="visibility_invisible">1</string>
<string name="visibility_gone">2</string>
<string name="product_info_footer_button_visibility">#string/visibility_visible</string>
</resources>
/res/values-large/visibilty.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="product_info_footer_button_visibility">#string/visibility_invisible</string>
</resources>
And then you can reference the visibility as follows for your button:
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="#string/product_info_footer_button_visibility" />
Warning: This depends on the device having the same enum values for visibility (0/1/2) as defined in the AOSP source. Device manufacturers and custom ROM creators can change these values, in which case this code will likely not work as desired.
The android:visibility attribute is an int (like many attributes) so you can do the following :
Define a resource file named visibility.xml in values-port and values-land resource directories. The content of this file is like this :
values-port/visibility.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="buttonvisibility">0</integer> <!-- 0 is the value for android:visible -->
</resources>
values-land/visibility.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="buttonvisibility">1</integer> <!-- 1 is the value for android:invisible -->
</resources>
and in your layout.xml :
<Button
android:id="#+id/btn_check_availability"
style="#style/product_info_footer_button"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:text="#string/check_availability"
android:visibility="#integer/buttonvisibility" />
It works : btn_check_availability is visible in portrait and invisible in landscape.
Note : this example use layout orientation as discriminator, but you can of course do it with any resource qualifier (like dimension, density, ...)
There is no magic expressions available in XML. If only.
There are two approaches to this problem:
a/ use the drawable folder system. Drawable folders can be copied and named to be DPI aware following the conventions dictated here: Supporting Multiple Screens.
b/ Do it programmatically. On runtime check for screen DPI and show/hide view accordingly.
Have you looked at using includes and multiple layouts organized into the appropriate size/orientation layout folders? Some layouts could either simply not have the button or have it hidden by default.
Re-using Layouts with include
Providing Alternative Resources
just started programming android and I am trying to make a simple xml file but it seems it got some problem with the android:text=#string/..
I have got this error:
error: Error: No resource found that matches the given name (at 'text'
with value '#string/ false_button').
For each of the strings..
The code is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding= "24dp"
android:text="#string/question_text"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button"/>
</LinearLayout>
</LinearLayout>
You need to have the resources that you want to reference in your layout in strings.xml under res/values/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="true_button">True Button</string>
<string name="false_button">False Button</string>
</resources>
#string/true_button refers to a resource in strings.xml
You can also have hardcoded string but not recommended
android:text="True Button"/>
add following to your string.xml file under values folder
<string name="true_button">true</string>
<string name="false_button">false</string>
Make sure that you are created string value with the name of false_button in the string.xml
<string name="false_button">false</string>
or just give name within the quotes "" in the android:text attribute
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="true_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="false_button"/>
Click on res folder then click on values and then open strings.xml and add following content.
<string name="true_button">whatever text you want to write to your button</string>
<string name="false_button">whatever text you want to write to your button</string>
and save your file. Then clean your project by clicking Project -> clean and then run it again.
Hope this will help you.
make sure that in res/values/
you have all the resources present there as follows
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="true_button">True</string>
<string name="false_button">False</string>
</resources>
otherwisely you can hardcode values at yours layout where you specified button as
android:text="True "/> or android:text="False"/>
In your android project, look for the res directory. Open it and inside it look for values directory. In values, open strings.xml and check if it has a flase_button value stored in it . If not you can add a new value using name as false_button and value as what you want to display on the button.
When you open the strings.xml in IDE like eclipse, you can switch views of the .xml file by selecting proper tab given at the bottom of your eclipse screen. Select strings.xml view and
modify the current strings.xml to
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">app</string>
<string name="false_button">FALSE</string>
</resources>
Hope this was helpful.
You just need to add this in your Strings.xml class
just add <string name="false_button">False Button</string> line in your strings.xml
so that it will look like :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="false_button">False Button</string>
</resources>
All of these responses that refer to adding the string to the string.xml file make sense and work, but fail to address the original problem: this book ("Big Nerd Ranch: Android Programming") goes through these steps one at a time, mentions referencing the string in the layout XML file, but fails to mention adding to the strings.xml file. Fail.
I need help displaying color from the next string resource to the textview
<string name="colored">
<b>Something working perfectly</b>
<font color="#F38">Something that doesn't work</font>
</string>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/colored"
/>
You have to set it programmatically, like this:
((TextView)findViewById(R.id.yourTextViewId)).setText(Html.fromHtml(getResources().getString(R.string. colored)));
XML:
<TextView android:id="#+id/yourTextViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
string.xml:
<string name="colored"><![CDATA[
<b>Something working perfectly</b>
<font color="#F38">Something that doesn't work</font>
]]></string>
Font color tag is not working right in string resources in Android. This is because of bug of parsing color in format like "#AARRGGBB". It is using Integer.parseInt(source, 16) to parse it (in com.android.internal.util.XmlUtils.convertValueToInt() method) which is accepting only signed int (so you can use range from #7FFFFFFF to #-7FFFFFFF).
Basically Color.parse() using Long.parse() instead and then just making Integer from that.
Also you cannot use your own color resources as references to font colors in strings because it is using System.getResources() method to find color resources. So only "android:color/[colorname]" will work.
In conclusion in font tag you can:
use Android system colors as references like <font color='#android:color/red'>Hello world!</font>
use basic colors like <font color='red'>Hello world!</font>
use signed integer which is painful because you need to convert negative values to not obvious format like <font color='#-0000FF00'>Hello world!</font> where #-0000FF00 is like #FFFF0100
use Html.fromHtml() method in runtime to parse HTML-like strings wrapped with CDATA tag like <![CDATA[<font color='#FFFF0000'>Hello world!</font>]]>
Why don't you define a color on the resources file and then use it on your layout file?
You can achieve this by defining something like on a colors.xml file (located inside the values folder):
<color name="black">#000000</color>
and then on your layout (xml)
android:textColor="#color/black"
I'm writing an application were it requires to change the language of the application as and when the user requires. The data in different language is stored in the data base, from which the data is fetched and the UI is updated. I'm want to what should be done if device does not support the particular language font. Any help will be much appreciated.Thanks in advance. _/|_
I don't know more about this but ...
for example if you want your application to support both English
and French strings (in addition to the default strings),
you can simply create two additional
resource directories called /res/values-en (for the English strings.xml) and
/res/values-fr (for the French strings.xml).
Within the strings.xml files, the
resource names are the same.
For example, the /res/values-en/strings.xml file could
look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello in English!</string>
</resources>
Whereas, the /res/values-fr/strings.xml file would look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Bonjour en Français!</string>
</resources>
A default layout file in the /res/layout directory that displays the string refers to the
string by the variable name #string/hello, without regard to which language or directory
the string resource is in.
The Android operating system determines which version of
the string (French, English, or default) to load at runtime.A layout with a TextView control
to display the string might look 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="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/hello" >
</LinearLayout>
The string is accessed programmatically in the normal way:
String str = getString(R.string.hello);
It’s as easy as that.
More you will found Here