5

I'm trying to use font resource directory and use different fonts in xml files in order to use them in preference. I read this thread but I couldn't solve the problem. I'm using support library v27 and I know it supports font in res instead of asset folder. Whenever I change the language of app it only uses just the font which is located in font-fa-rIR folder. I want application uses different fonts for different type of languages respectively. I have attached an image from res directory of application. Where is my problem? How can I solve it? Thank in advance.

preference_button.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp">

    <TextView
        android:id="@android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/font_1" />

    <TextView
        android:id="@+id/summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/font_1" />

</LinearLayout>

Font resource directory

Effect of font on English language, as you can see the numbers are still in Farsi which means it uses Farsi font

Effect of font on Farsi language

Mohsen Hatami
  • 317
  • 3
  • 14

1 Answers1

1

May be This Que Helps You

Ref: Chnaging Locale It self

Locale locale2 = new Locale("fr"); 
Locale.setDefault(locale2);
Configuration config2 = new Configuration();
config2.locale = locale2;
getBaseContext().getResources().updateConfiguration(
    config2, getBaseContext().getResources().getDisplayMetrics());

Font From Res

TextView tv_the = (TextView) findViewById(R.id.the);
Typeface face = Typeface.createFromAsset(getAssets(), "font.ttf");
tv_the.setTypeface(face);

For More Info : How to set custom font in .xml file instead of .java file?

Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65
  • Thanks but I have a class which can change Locale even in runtime. I'm confused how to use fonts in res just same as layouts and other resources. – Mohsen Hatami Aug 26 '18 at 09:32
  • Thanks, It is a great idea to use modified TextView inside preference xml files. In this case, I don't need to use font resource directory. Thanks an ocean – Mohsen Hatami Aug 26 '18 at 09:46
  • 1
    @Ashvinsolanki Changing Locale Itself is not fetching the font which is available in "font-fr" folder. On Restart of application it is taking that font, but not on runtime. How to do that – praveen2034 Apr 09 '19 at 13:30
  • @user137021 may this will help you https://stackoverflow.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated – Ashvin solanki Apr 09 '19 at 13:39