Question

I'm trying to sets textview text into a custom font.

In order to do so, I've done the next steps -

1) The layout Xml is looking like so:

           <TextView
                android:id="@+id/textView_window_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="Profile"
                android:textColor="#505A62" />

2) I've created a fonts folder inside the assets folder and place 3 diffrent fonts file. All three of them are a .ttf file types. Also the name of each font file and type are written in small letter, like so - cr.ttf

3) At the fragment class I've used the next code -

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

            View v = inflater.inflate(R.layout.gd_menu_layout, container, false);

             Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),
                "fonts/cr.ttf");
             titleWindow = (TextView) v.findViewById(R.id.textView_window_title);
             titleWindow.setTypeface(tf);


             return v;

        }

I've tried it with 3 diffrent fonts files but none of them change the textview font.

Any ideas why this code dosen't work?

Thanks for any kind of help

Was it helpful?

Solution

Try this lib: https://github.com/neopixl/PixlUI

It is fast to try it and see it's magic :)

OTHER TIPS

String fontPath = "fonts/cr.ttf";
Typeface tf = Typeface.createFromAsset(this.getAssets(), fontPath);
titleWindow.setTypeface(tf);

try this

Set your typeface fonts on all views you are using ,just try my codes...

private EditText etComment2;
private TextView tvQuestionText1;
private TextView tvQuestionText2;
private Button btn1; 


     in you onCreate()

    etComment3 = (EditText)findViewById(R.id.etComment3);
    tvQuestionText1 = (TextView)findViewById(R.id.tvQuestionText1);
    tvQuestionText2 = (TextView)findViewById(R.id.tvQuestionText2);
    btn1 = (Button)findViewById(R.id.btn1);

create array of your all views here-

View allViews[] = 
        {
            etComment2,
            tvQuestionText1,
            tvQuestionText2,
            btn1
        };

pass this view object here- AppConstant is our class to manage all static methods

    AppConstant.setTypeFace(allViews);

Now here is our constant class with method TypeFace

public static void setTypeFace(View []views)
{

    for(int i=0 ; i<views.length; i++)
    {
        Typeface tf = Typeface.createFromAsset(views[i].getContext().getAssets(), "fonts/SourceSansPro-Semibold.ttf");
        if(views[i] instanceof TextView )
        {
            ((TextView)views[i]).setTypeface(tf);
        }
        else if(views[i] instanceof Button )
        {
            ((Button)views[i]).setTypeface(tf);
        }
        else if(views[i] instanceof EditText )
        {
            ((EditText)views[i]).setTypeface(tf);
        }
    }
// Declare styleable in attrs.xml
/res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTextView">
<attr name="fontName" format="string" />
    </declare-styleable>
</resources>

Create a layout in layout folder

    /res/layout/activity_main.xml
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:customfontdemo="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:padding="12dp"
            android:text="Standard Android Font" />

         <com.authorwjf.customfontdemo.MyTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="32sp"
            android:padding="12dp"
            customfontdemo:fontName="pipe_dream.ttf"
            android:text="Custom Android Font" />

    </LinearLayout>

//Now Create your Custom Textview

/src/MyTextView.java

package com.deepak.utilsview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);

    }

    public MyTextView(Context context) {
        super(context);
        init(null);
    }

    private void init(AttributeSet attrs) {
        if (attrs!=null) {
             TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyTextView);
             String fontName = a.getString(R.styleable.MyTextView_fontName);
             if (fontName!=null) {
                 Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontName);
                 setTypeface(myTypeface);
             }
             a.recycle();
        }
    }

}

//now see the fonts in your activity after running activity

/src/MainActivity.java package com.authorwjf.customfontdemo;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top