1

I have a single TextView with two languages: Arabic and English.

I have two different fonts, arabic.ttf and english.ttf.

I want to apply the two fonts to the single TextView and it should change the fonts depending upon the language.

For Arabic only arabic.ttf should be applied and for English english.ttf should be applied.

The problem is that both of them are in the same TextView so I am having trouble applying two fonts.

I checked the example of SpannableStringBuilder, but it did not help.

Please provide a working code.

Furqan Hussain
  • 135
  • 1
  • 4
  • 14

3 Answers3

3

The easiest way to achieve what you want:

Create 2 folders:

  • res/font-en
  • res/font-ar

Put your english font file inside the -en folder and arabic font file inside -ar folder.

Rename the files in order to have the same name in each folder e.g.

textViewFont.ttf

In your TextView add android:fontFamily="@font/textViewFont"

This way, when your app is opened, the XML framework will load the font based on the current Locale

Sal
  • 814
  • 6
  • 12
  • 1
    i want both the fonts at the same time – Furqan Hussain Dec 31 '17 at 15:49
  • As far as I know you can't do this with only one view, since the style is applied to the view object itself, rather than the text. – Sal Dec 31 '17 at 15:59
  • ok, so when I apply arabic font,the english text is not affected.However the all the spaces in the english characters are gone.For example when i apply the arabic text,the arabic font changes but the english text which was "sample text font" is now being displayed as "sampletextfont".Why these spaces are gone? – Furqan Hussain Dec 31 '17 at 16:06
  • Probably because the Arabic font is designed for Arabic text characters and this affects the English words by removing spaces in this case. But this is my opinion. – Sal Dec 31 '17 at 16:52
  • Did you understand the question? – Ahamadullah Saikat Mar 04 '20 at 06:28
0

You will have to be extending your custom typeface fonts to TypeFaceSpan and also use the SpannableString to help you get that fixed. The link, https://stackoverflow.com/a/9619371/8208341 , helped me.

Also split your textview texts into array of strings then loop through to apply your method created above.

0

how about create new style style-ar.xml in values-ar

<style name="my_arbic_style">
    <item name="android:textSize">18sp</item>
    <item name="android:fontFamily">sans-serif-thin</item>
</style>

and in your layout:

<TextView
    android:text="khaled djeriou"
    android:textColor="@color/white"
    android:gravity="center_vertical`
    app:theme="@style/my_arbic_style"/>

i hope this work

Noa Nash
  • 1
  • 1