Change textSize with another language

I added Spanish and French to my application, but some wording is longer in Spanish and then in English. how can I change the text when the values-es / string.xml file is available.

+4
source share
3 answers

You can use the resources file of the dimens.xml for this purpose. In your case, you probably want to create a file called res/values-es/dimens.xml and possibly also a version of -fr . You can specify default values ​​in res/values/dimens.xml (or res/values-en/dimens.xml if you want to be more specific).

An example from the Additional resources sections on developer.android.com:

diameter.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); 

SO also has solutions that use an iterative / recursive process to reduce the size of the TextView text to “fit” in its bounding box (using a custom view), but I would say the above is a more robust approach, especially if you plan to add more languages ​​in the future.

+6
source

You need to specify a different layout format in the layouts. That way, when Android pulls out of value-es / string.xml, it will load various layout-es / yourfile.xml. This layout file can then indicate the theme, style, or size of the text in the views.

0
source

The above explanations are correct, but they do not fully explain how to do this.

When you open your project in Android Studio, it automatically shows this project in Android mode. You need to click the “Android” tab in the upper left corner of Android Studio and select “ Project ”. Then you need to go to " application> src> main> res ". Then you need to right-click in the res folder, and in the menu that appears, select " New> Android resource directory . A dialog will appear, and for the directory name: enter -es and click OK .

This will create a folder for all Spanish values. And then you can right-click in this values-es folder to create the dimensions.xml , string.xml , color.xml , ... etc. files to be used whenever Spanish is selected on the phone.

If you have already created the string.xml file for the Spanish Locale via the graphical user interface, then the values-es folder with the string.xml file will already be in the project when you go there. And in this case, you just need to right-click in the values-es folder to create a file of a diameter.xml for the Spanish language.

0
source

Source: https://habr.com/ru/post/1389762/


All Articles