js salat
js salat

Reputation: 53

Can i change the language of android app?

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.

Upvotes: 3

Views: 349

Answers (2)

Sandeep
Sandeep

Reputation: 3344

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...

Upvotes: 0

Bhavin
Bhavin

Reputation: 6010

First of all copy font in assets folder, then write

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

and then :

text.setTypeFace(tf);

Upvotes: 4

Related Questions