0

I am not able to understand why, despite mentioning the utf-8 encoding in the string.xml code, the "ddd" is not being encoded to the required font (DevLys 180, a Hindi font).

I've copied this code from the internet to get Hindi to work in my app:

Main.Activity java:

package com.example.spk.muni_darshan;

import android.app.Activity;
import android.graphics.Typeface;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView tv = (TextView) findViewById(R.id.titleText);
Typeface font = Typeface.createFromAsset(getAssets(),
        "font/DevLys 180.ttf");
tv.setTypeface(font);

}

string.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>
           <string name="app_name">Muni_Darshan</string>
           <string name="font">font</string>
           <string name="title">ddddd</string>
</resources>

activity_main.xml:

<TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/title"
          android:id="@+id/titleText"
          android:textSize="@dimen/abc_action_bar_default_height_material"
          android:fontFamily="@string/font" />
Laurel
  • 5,965
  • 14
  • 31
  • 57

1 Answers1

0

When you are setting the font in the code via setTypeface, you may not have to set the fontFamily option in the xml. Specifically:

android:fontFamily="@string/font"

You can try removing this.

Also, make sure you are packaging the fonts in assets/font folder. You can unzip the APK to verify that.

AKAIK: There is no direct way to specify custom packaged fonts in the XML. It has to be done by code.

What I usually do is make a derived class of TextView and use that in the xml instead. In the constructor of this new class you have set the typeface from the code.

sushantsha
  • 81
  • 1
  • 7
  • i have copy pasted the ttf file in the font folder.its location:app/src/main/assets/font.have removed the fontfamily code.Do you see any other reason for the above code's failure to deliver hindi font? – VKothari Mar 23 '15 at 12:11
  • 1) Keeping in assets/font does not gaurantee that it will be packaged in the APK. Your build config might not know about it. Just try to unzip the built APK file and see if you have the font lying there? (compressed) 2) Verify that font file is sane. Try opening the file on a windows machine maybe? – sushantsha Mar 24 '15 at 07:29
  • Converted the .iml file to .rar but could not extract it afterwards.There is no .apk file.Can you please guide a little further.thanks – VKothari Mar 27 '15 at 14:25
  • apk file is final package you push to your device/emulator. – sushantsha Apr 01 '15 at 07:00