change textSize with different language locale - android

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.

Related

Show Layouts in Android Studio depending on Flavor

I have a project in Android Studio that uses different flavors for different colors (some other things as well, but they don't matter).
What I would like is to see how a certain UI element (all defined by xml files) looks with non-default flavor?
Let's say I have to flavors, flavor A which is default and flavor B which is not. Lets say flavor A main color is red, and flavor B main color is green.
When I open layouts, they will always show as red, no matter what flavor in build variants is selected.
I'm sure there is a way to do this, but I don't know how.
Edit1: Seems I didn't explain it (thought it was implicit).
My resource files for flavors are already in different folders. Colors in resource files are of the same name. That is not the problem, the problem is that I can't see colors in layouts inside of Android Studio.
Colors are in separate folders and it works as it should. When I build and install flavors, they use their colors. But when I open a layout, it always shows with default colors in Android Studio
Let me blow out some dusts first :
You're getting default colors in your layout files is because, your layouts resides under default folder (main package in this case).
What happens here is that, when you create build of particular flavor (let's say flavorA), packaging system takes files specific to that flavors that are duplicated from default folder and replace it to provide you flavor-specific build.
So, your generated apk file contains flavor specific files (colors.xml in your case) + default files from main (res folder of main like all layouts and other resources & your class files too) that are not the part of your flavor specific folder.
That's why everything works perfectly.
Now back to point : (Hack)
What should you do to see layouts with flavor specific colors?
"Although I would not recommend this way" unless you're having different layout logic for flavor specific build, all you can do is place particular layout files (just to see rendered output) in your layout folder under your flavorA directory.
So, It'll now take colors from flavorA folder and render you layout in IDE with that colors in your layout file.
Note: Again, this is just a hacky way to see different things in flavor specific way, not a solution. Even though I don't think there's even solution for this problem because, this is not a problem & this is how multi-flavor gradle build system works and that's not a bug in this system.
Edit:
So, after some research, I find out how you can achieve such thing that 'you get rendered output based on your flavor colors'.
Let's say you're having different colors based on your flavors like below :
Now, contents of flavorB & flavorW are different for colors.xml file but having same color attributes like:
flavorB contains :
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#757575</color>
<color name="colorPrimaryDark">#212121</color>
<color name="colorAccent">#607d8b</color>
</resources>
flavorW contains :
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6d4c41</color>
<color name="colorPrimaryDark">#3e2723</color>
<color name="colorAccent">#33691e</color>
</resources>
As you can find out that number of color elements are the same but just values are different.
TL;DR
Solution : In such case, you can simply delete colors.xml from your main/res/values folder. It won't generate any error unless you're missing any flavor specific color attr for your "current build variant".
check out main dir for my project :
(As you can see here that, main resource directory doesn't contains any colors.xml file)
So, if you'll try changing your build variant and look at any layout file from main it'll opt out with that specific product flavor color for you.
Note : Beware that, If you add or remove one color from one flavor remember to also add/remove it to other flavors too or else your project might get error while building. (you can also do the same trick with other resources too like drawables, layouts, dimensions etc. just make sure every flavor contains it all with the same resource name)
Im not sure if its possible to affect layout preview with flavours, but its definetely possible with themes, so let me suggest a workaround.
1) Create two themes in your main source set:
<style name="MyStyle">
<item name="colorPrimary">#44f</item>
</style>
<style name="MyStyle.Red">
<item name="colorPrimary">#f44</item>
</style>
That is basically enough to customise layout preview: you can select theme you want.
2) To make your layouts flavor-dependent, create a theme that will inherit either MyStyle or MyStyle.Red depending on flavor
In main source set:
<style name="MyFlavorDependentStyle" parent="MyStyle"/>
In flavor source set:
<style name="MyFlavorDependentStyle" parent="MyStyle.Red"/>
and use MyFlavorDependentStyle in your layout.
if the colors are defined in XML just move the colors definition from main/colors.xml to flavorA/colors.xml and flavorB/colors.xml

TextView showing strings that are not in the strings.xml when device language is changed

When I change the device language, strings that are not in the strings.xml are showing in my text view.
Below is my text view code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="#dimen/padding_all_activity_16"
android:text="#string/check_document_image_readable"
android:textSize="18sp"
android:textStyle="bold" />
......
in the values/string.xml file:
<string name="check_document_image_readable">A foto ficou legível?</string>
My app don't support other languages (it is written in portuguese).
When I run the app withe the device set to English:
But if I change to Spanish (even if I kill the App):
I'm developing with Android Studio 3.0.1. This happens in the API's 24 and 25.
How can I use only the values/strings.xml for any device language?
Android has support to Different Languages and Culturesaccording to the device's language setup. You can achieve that working with different strings resource for which language that you desire to support.
In your project, right-click res/values/ and select New -> Values resource file. Enter "strings" for the file name and leave the source option set to main. In the Available qualifiers and click ">>" button to move "Locale"to the Chosen qualifiers section. Select the language that you wish to support and select the region (default is: Any region) and left-click "Ok". Android studio will automatically generate a new resource file also called "strings.xml".
Why did I mention that? Android at run time looks for a default string resource. What might be happening is that you're not using a default string resource, instead you're using a 'string-pt' resource, this would be a problem if you don't have resource string file for Spanish or other language.
if you intend to support other languages when the user changes the default language.
you should override string.xml file
create another string.xml (sp) with Spanish as local and add your resource value
<resources>
<string name="check_document_image_readable">A foto ficou legível?</string>
</resources>
to override string.xml with the Spanish version.
click on the res folder choose new resource file and name it strings.
on the left select local and choose Spanish

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

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.

android drawable selection custom

To better explain: For example i have two languages available. Thats why i will have image for English and image for Russian. Both of them i reference in one drawable xml file.
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/image_eng"></item>
<item android:drawable="#drawable/image_ru"></item>
</selector>
What type of drawable xml files i should use ?
How can i set and detect state and switch image ? Basicly it works like android:state_
If I understand correctly, you want to use one image when the phone is in English, and the other when the phone is in Russian. You should use the language modifiers on the drawable folder, not in the drawable name.
So, you would have a folder named drawable-en, which would contain your image for English (drawable-en/image.png, for example). Then, have another folder named drawable-ru which contains your Russian version of the image, but it must have the same name (drawable-ru/image.png).
Then, in your layout XML (or wherever you're referencing the drawable) just reference #drawable/image ... Android will automatically choose the appropriate image.
Please read how Android resource Localization work.
You don't have to use selector for that , or force anything.
just put your image file in the following folders:
res/drawable-en
res/drawable-ru
and in your code you can use:
Drawable draw = getResources().getDrawable(R.drawable.image);
The app will select the suitable one based on the phone's language

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