3

I am developing news application for Gujarati Language. Now my problem is that its working well but it shows Squares([]) instead of fonts, so how can i make it visible in Gujarati language so it can display Gujarati fonts .

Thank you in advance.

js salat
  • 53
  • 1
  • 5

2 Answers2

4

First of all copy font in assets folder, then write

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/gujaratifont.otf");

and then :

text.setTypeFace(tf);
Bhavin
  • 6,020
  • 4
  • 24
  • 26
  • 1
    Yea both otf and ttf can be used. – Bhavin Mar 29 '12 at 11:49
  • I tried this but its not giving proper output it still shows me squares. – js salat Mar 31 '12 at 04:16
  • insertData("3","hello.jpg","'સુપર ફાઇટ લીગ' ના આગામી ઉદ્ઘાટન સમારોહમાં 'અનારકલી' ધમાલ મચાવશે ","This is the third item"); TextView cap = (TextView) view.findViewById(R.id.caption); Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/gujaratifont.ttf"); cap.setTypeface(tf); – js salat Mar 31 '12 at 06:44
  • If this does not work Save it in String.xml file. like 'સુપર ફાઇટ લીગ' ના આગામી ઉદ્ઘાટન સમારોહમાં 'અનારકલી' ધમાલ મચાવશે – Bhavin Mar 31 '12 at 06:49
0

This is the way to change the font typeface using xml.

  1. Define a new NameSpace: xmlns:your_namespace="http://schemas.android.com/apk/res/com.nound.test" (“com.nound.test” is the package name defined in Manifest.xml)

  2. Define the custom attribute(s) in /res/values/attrs.xml file.

    <resources>
        <declare-styleable name="TypefacedTextView">
             <attr name="textStyle" format="string"/>
        </declare-styleable> </resources>
    
  3. Java class that extends TextView... as follows public class TypefacedTextView extends TextView {

    public TypefacedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        //Typeface.createFromAsset doesn't work in the layout editor. Skipping...
        if (isInEditMode()) {
            return;
        }
    
        TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView);
        //String fontName = styledAttrs.getString(R.styleable.TypefacedTextView_typeface);
        String fontNameForBold = styledAttrs.getString(R.styleable.TypefacedTextView_textStyle);
        styledAttrs.recycle();
    
        if (fontNameForBold!=null && fontNameForBold.equals("bold")) {
            Typeface typeface_bold = Typeface.createFromAsset(context.getAssets(), "fonts/tahoma/tahoma_bold.ttf");
            setTypeface(typeface_bold);
        }else{
            Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/tahoma/tahoma.ttf");
            setTypeface(typeface);
        }
    }
    
  4. here is the layout code where you wanna change the font typeface. in this "com.nound.test.ui" is the above (3 point) java file package name. TypefacedTextView is class name.

<com.nound.test.ui.TypefacedTextView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:your_namespace="http://schemas.android.com/apk/res/com.nound.test"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Custom fonts in XML are easy_bold font"
            your_namespace:textStyle="bold" />


<com.nound.test.ui.TypefacedTextView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:your_namespace="http://schemas.android.com/apk/res/com.nound.test"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Custom fonts in XML are easy_regular font"
            />

its enough to do...

Noundla Sandeep
  • 3,334
  • 5
  • 29
  • 56