5

I have an application where I defined all the necessary detail, now I want to make it LOCALIZED. I know we have to make value folder and add string where the string of the language other language is declared with same "key".

My question is suppose the heading of my app is "DASHBOARD" which in Spanish is "Tablero de instrumentos" know this contains more letter then we have in ENGLISH so the symmetry of the app gets disturb. What I want is to customize the font-size, according to the Language called, so that it doesn't affect my UI.

James
  • 4,573
  • 29
  • 32
Unique
  • 51
  • 4
  • You could use 3rd party libraries for this. I read about some library on the android arsenal. – Sufian Dec 14 '15 at 11:15
  • Create `values-en(for English)` and `values-es(for spanish)` folder under `res` directory, Then create `dimens.xml` file under both `values-en` and `values-es` folder. and specify create resource specify font size in it. Now use this `dimen` size to set size in widget. – Dhaval Patel Dec 15 '15 at 04:51

3 Answers3

0

I think the solution for you would be here: How to adjust text font size to fit textview

According to this topic you can use:

speedplane's FontFitTextView, but it only decreases font size if needed to make the text fit, and keeps its font size otherwise. It does not increase the font size to fit height.

Note: speedplane is a nick of a guy from StackOverflow forum.

Code: FontFixTextView.java

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94
0

You might want to try this

String language = Locale.getDefault().getDisplayLanguage();

if ( language.equalsIgnoreCase("Spanish") {
    myTextView.setTextSize( font_size );
}
Mann
  • 560
  • 3
  • 13
0

You can do this by making dimens file for different languages/locales and defining the custom name for your dimensions, with different values for different locales/languages.

Like I have created 3 files with name dimens in values folder of resources directory, but each one is defined for different locales.
enter image description here.

You can create this file by right clicking on res -> New -> Android resource File. And fill the details like this.
enter image description here Do this to create different files for different local/languages.

And for different language I have different values. like- enter image description here For English language I have these values and enter image description here for hindi I have these values.

as you can see the name of dimen key are same in all both files but values are different.

Now you can get the values in your code by:-

val lineHeight = context.resources.getDimension(R.dimen.H6_lineHeight)

Now this will not give the value you have mentioned in your filem rather this will give values in pixel.

Now you can use this function to get values in sp. You can use your converter function in place of this to convert the value into your desired unit.

fun pixelsToSp(context: Context): Float {
    val scaledDensity = context.resources.displayMetrics.scaledDensity
    return this / scaledDensity
}

Now the android will handle and pick values from corresponding files based on locale/language of working device.

Abhyam Gupta
  • 147
  • 4
  • 4