How to use tools:locale for strings.xml files? - android

Background
Lint has a relatively new feature, so that it will warn us about missing translation only for languages that we choose, but i don't get how to use it.
The problem
for some reason, Lint still warns me about languages that i don't intend on translating yet.
What i've tried
for example, currently i want to only have 2 languages : english ("en") and hebrew (which is sadly both "iw" and "he" ) .
so i have strings files in the folders :
values (for english)
"values-he" and "values-iw" (for hebrew) .
i've tried putting the new attribute in the english file as such :
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en,iw,he">
...
The question
what is the right way to do it?

Looking here it seems that it's to be used into resource files to indicate the default language. So you can specify only one locale code.
should correspond to a language
Moreover it seems to be used only to disable spell-checker

If you read the article:
This lets you tell the tools which language you're using in your
base values folder. For strings in for example values-de or values-en it's obvious, but not in the base "values" folder
It need only to know what is the language in the default "values" folder (the folder without any attribute).
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en">

You are already in right direction. Just need some modification. Like this manner:
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="es">
Now we know that the language used for strings in the default values folder is Spanish rather than English.
Used by: Lint, Studio (to disable spell checking in non-English resource files)
Reference Link: Go to here tools:locale
Thanks.

Related

Enable Android Studio spell checking on language specific strings.xml files

In Android Studio, spellchecking seems to be disabled for language specific strings.xml files (and I understand why it would be disabled by default).
But is it possible to enable it, to check values, for a couple of those files? ("fr/strings.xml" for example), or at least run it on demand.
To avoid or to enforce spellchecking you could add tools:locale to the <resources> tag in your string xml.
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en">
This tells Lint your resources' locale.
Source
Yes, go to Android Studio Preferences > Editor > Inspections > Spelling > Typo (check the box). Also make sure all scopes have been selected.
Incorrect spellings in the strings file show up with a light green line, clicking on which would show a message that it may be a typo.

Text in android app (beginner...)

My problem is very simple: I've started an app for playing Darts. The app will have several activities ('pages').
One page will be about the rules of the game. I'll be using a scroll layout because it's quite some text. But how to get the text there?!
I assume working with strings is not the best way? Do I use the XML file to get the text on screen then or does it work via Java (Assetmanager)?
Maybe there are sample apps in which large chunks of text are used?
I know this really might seem like a trivial question but I haven't a clue where to begin.
Thanks in advance!
You should put your string in your strings.xml in your res\values folder.
You can define strings by ID which allows easier internationalization (i18n), so that you can easily adjust the strings used in your app to locale (which is done automatically using resource identifiers, and it falls back to strings.xml if it can't find a strings-hu.xml in case you have Hungarian locale set as system language).
You can also define string-array and the like in XMLs. Then all you need is create a layout XML with a ScrollView in it that has a TextView in it and then you set android:text="#string/rules" for that TextView and you're done.
It is so simple my friend.
You can simply use TextView and in "android:text" you refer to the string that you delared in strings.xml file (by its name)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="#string/text_name"
/>
If your text is dynamic, you can modify it in Java code!
Make a String Resource like this.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1"> your text </string>
<string name="string2"> your text </string>
</resources>
and access like this if you are in Activity.
getResources().getString(R.string.string1);
If you are a begginer you should read some tutorials after post a question...
I give you a three nice tutorials below :
Want to Learn How to Program for Android? Start Here
Android Programming Tutorial
Android Development with Android Studio or Eclipse ADT
About your question, if you don't know how to use the string.xml resource just read the string-resource guide
Hope it helps.

change textSize with different language locale

I have added spanish and french to my app but some of the wording is longer in spanish then english. how can i change the textsize when the values-es/string.xml file is accessed
You can use the dimens.xml resource file for this purpose. In your case you'll probably want to create a file called res/values-es/dimens.xml, and possibly also a -fr version. You can specifify the default values in res/values/dimens.xml (or res/values-en/dimens.xml, if you want to be more specific).
Example grabbed from the More Resource Types section on developer.android.com:
dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textview_height">25dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="ball_radius">30dp</dimen>
<dimen name="font_size">16sp</dimen>
</resources>
Apply in xml
<TextView
android:layout_height="#dimen/textview_height"
android:layout_width="#dimen/textview_width"
android:textSize="#dimen/font_size"/>
Or in code
float fontSize = getResources().getDimension(R.dimen.font_size);
There are also solutions here on SO that use a iterative/recursive process to shrink the text size of a TextView to 'fit' in its bounding box (using a custom view), but I'd say above is a more robust approach, especially if you're considering adding more languages in the future.
The above explanations are correct, but they don't fully explain how to do it.
When you open your project in Android Studio, then it automatically shows this project in the "Android" mode. You need to click on the "Android" tab in the top, left corner of Android Studio and select "Project". Then you need to go into "app>src>main>res". Then you need to right-click on the "res" folder and from the menu that comes up, select "New>Android resource directory". A dialogue will come up, and for Directory name: type in values-es and click OK.
This will create a folder for all Spanish Locale values. And then you can right-click on this values-es folder to create dimens.xml, string.xml, color.xml, ...etc. files that will be used whenever Spanish Locale is selected in the phone.
If you've already created a string.xml file for Spanish Locale through the graphical user interface, then the values-es folder with string.xml file will already be in the Project, when you go there. And in this case, you just need to right-click on the values-es folder to create the dimens.xml file there for Spanish Locale.
You would need to specify a different layout file in layout-es. That way when Android pulls from values-es/string.xml, it'll load the different layout-es/yourfile.xml. That layout file can then specify a theme, style, or text size on the views.

Problems with res folder and R.java

I'm doing Tutorials and I'm on section about images. It says to put them into the folder res/drawable. But I don't have that folder, I have three instead: res/drawable-hdpi, res/drawable-ldpi and res/drawable-mdpi. So whats the difference between them?
Im using this tutorial.
One of the steps is:
Create a strings.xml file in
res/values/ and edit the file to look
like
There already is strings.xml, combined with the above, telling me to use res/drawable, are these tutorials out of date?
This tutorial has code like:
R.id.spinner
R.array.planets_array
R.layout is just simple enum. Uses the main.xml in the layout folder. But where are R.id and R.array to come from. Because it is coming up in eclipse saying it doesn't know what they are. R.java gets updated automatically, so can someone tell me from reading that tutorial where id gets added to R? It says that
The R.array.planets_array ID
references the string-array defined
above
Only it doesn't work. I doubt it makes a difference that i didn't make strings.xml since it's the same filename in the same location. But since R.java is meant to be updated automatically I don't know how to fix this.
Those are for the different screen resolutions for the range of devices that are out there. Read about supporting multiple screens on the Android dev site.
Just so you know where the R stuff comes from.
The R.java file is a generated file which contains some kind of pointers to a resource in your application. It is a simple integer actually which uniquely identifies the resource in the internal resource management system of Android.
R.string identifiers are generated from resources XML files like this one for example.
<resources>
<string name="test">This is a test string.</string>
</resources>
R.array identifiers from string array XML files.
<resources>
<string-array name="days_of_week">
<item>Monday</item>
<item>Tuesday</item>
<item>Wednesday</item>
<item>Thursday</item>
<item>Friday</item>
<item>Saturday</item>
<item>Sunday</item>
</string-array>
</resources>
You can access that array using its identifier R.id.days_of_week now.
R.id identifiers are a bit special.
They are generated in two ways. The first one is when you define a View in your XML layout file using the #+id/... syntax. Note the + sign.
The other way is to define them in resource XML files like strings for example.
<resources>
<item type="id" name="first" />
<item type="id" name="second" />
</resources>
You'd then just use them in a layout XML file like this #id/first. Note that there is no + sign anymore as you reference it, before you were declaring it.
In code you then use it like this, R.id.first.
There are a lot of other resources. I'd like to point you to the Application Resources article and also make sure to checkout the Resource Types sub-article.
If you don't have the folder, just create it. It is basically the fallback for the case that you don't have a resource in a more specific folder like res/drawable-hdpi
The *-xx folders allow you to provide more specific drawables (images) for various screen resolutions.
The same principle applies to values/ and values-xx/ where xx is a country code ; the xx versions allow you to have translations for UI messages.

Eclipse + Android not recognizing my (dimension) values

I am teaching myself Android using Eclipse, the Android plug-in, and Sams "Teach Yourself Android Development" book. I have this weird little problem. I've been able to create xml files in the res/values directory that hold strings and color values (colors.xml and strings.xml). I have been able to reference these values in the properties of my Android screens (the xml in res/layout), for example setting the "Text" and "Text color" properties with references like "#string/topTitle" and "#color/titleColor," where topTitle and titleColor are defined in the xml files.
BUT: when I create a file called "dimens.xml" and have font sizes in it, Eclipse correctly puts this file in res/values, but when I try to reference these values, e.g. "#dimension/titleFont" I get an error "No resource found that matches the given name." I've tried lots of different names, I've tried "#dimens" instead of the type, still nothing. If I go into the layout xml file and set it explicitly to a font size, e.g. 22pt, it works.
So Eclipse recognized my "dimens.xml" file when I made it well enough to put it in res/values, and lets me edit it, and shows it full of (dimension) values. It just doesn't recognize my referring to it in other xml files.
The book I'm using doesn't actually show a dimension example so I must be doing something wrong. I checked the Android docs but couldn't see any problem.
Any help appreciated. Thanks.
The correct way to refer to a dimension variable (stored in your dimens.xml (don't think the name here really matters though, it's what's inside that does)) from another xml file is like this:
"#dimen/nameOfVariable"
Notice that it is neither dimension, dimensions or dimens, but dimen!
If you look inside your xml file where you have your values, this will make sense as dimen is the name of the xml elements storing dimension values:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<dimen name="someDimension">5dp</dimen>
<dimen name="anotherDimension">10dp</dimen>
</resources>

Categories

Resources