Ali Allahyar
Ali Allahyar

Reputation: 341

How to set typeface for the text view xml layout

I have created an XML layout with the name def_list. I have assigned an id for it and that is def_list_textview. I have put a font called bn.ttf in the assets folder.

The XML layout is as follows:

<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/def_list_textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="right" >
    </TextView>

But when I set a typeface for it using the code:

def_list_tv = (TextView)findViewById(R.id.def_list_textview);
Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/bn.ttf");
def_list_tv.setTypeface(tf);

I get a nullPointerException in the LogCat. I don't know why.

This is my LogCat:

12-26 17:14:56.496: E/AndroidRuntime(1254): FATAL EXCEPTION: main
12-26 17:14:56.496: E/AndroidRuntime(1254): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.adeveloper.handydic/net.adeveloper.dic.PortDef}: java.lang.NullPointerException
12-26 17:14:56.496: E/AndroidRuntime(1254):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-26 17:14:56.496: E/AndroidRuntime(1254):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-26 17:14:56.496: E/AndroidRuntime(1254):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-26 17:14:56.496: E/AndroidRuntime(1254):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-26 17:14:56.496: E/AndroidRuntime(1254):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 17:14:56.496: E/AndroidRuntime(1254):     at android.os.Looper.loop(Looper.java:123)
12-26 17:14:56.496: E/AndroidRuntime(1254):     at android.app.ActivityThread.main(ActivityThread.java:3683)
12-26 17:14:56.496: E/AndroidRuntime(1254):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 17:14:56.496: E/AndroidRuntime(1254):     at java.lang.reflect.Method.invoke(Method.java:507)
12-26 17:14:56.496: E/AndroidRuntime(1254):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-26 17:14:56.496: E/AndroidRuntime(1254):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-26 17:14:56.496: E/AndroidRuntime(1254):     at dalvik.system.NativeStart.main(Native Method)
12-26 17:14:56.496: E/AndroidRuntime(1254): Caused by: java.lang.NullPointerException
12-26 17:14:56.496: E/AndroidRuntime(1254):     at net.adeveloper.dic.PortDef.onCreate(PortDef.java:53)
12-26 17:14:56.496: E/AndroidRuntime(1254):     at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-26 17:14:56.496: E/AndroidRuntime(1254):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-26 17:14:56.496: E/AndroidRuntime(1254):     ... 11 more

Upvotes: 3

Views: 6706

Answers (9)

Nitin Zagade
Nitin Zagade

Reputation: 71

public class TextViewPlus extends TextView {
    private static final String TAG = "TextView";

    public TextViewPlus(Context context) {
        super(context);
    }

    public TextViewPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
        String customFont = a.getString(R.styleable.TextViewPlus_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    public boolean setCustomFont(Context ctx, String asset) {
        Typeface tf = null;
        try {
            tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
        } catch (Exception e) {
            Log.e(TAG, "Could not get typeface: "+e.getMessage());
            return false;
        }

        setTypeface(tf);  
        return true;
    }
}

Upvotes: 0

it&#39;s me
it&#39;s me

Reputation: 188

Can you please try this... and I think some thing wrong in your path check the path

def_list_tv = (TextView)findViewById(R.id.def_list_textview);
Typeface tf = Typeface.createFromAsset(this.getAssets(), "bn.ttf");
def_list_tv.setTypeface(tf,null);

Upvotes: 0

Revathy
Revathy

Reputation: 97

def_list_tv = (TextView)findViewById(R.id.def_list_textview);   //line no 1
System.out.println("Textview==="+def_list_tv);
Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/bn.ttf");
def_list_tv.setTypeface(tf); 

If you see Textview===null in your Logcat,specify the correct textview id in line no 1.I got the same error which u got when did this mistake.I hope this will helpful for you.

Upvotes: 0

Abhishek Dhiman
Abhishek Dhiman

Reputation: 1651

Try Creating a CustomTextView like the one below:

public class CustomTextView extends TextView {

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

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

public CustomTextView(Context context) {
    super(context);
    init();
}

private void init() {
    if (!isInEditMode()) {
        Typeface typeFace = Typeface.createFromAsset(getContext().getAssets(), "fonts/arial.ttf");
        setTypeface(typeFace,Typeface.NORMAL);
    }
}

}

And please your fonts file in assets/fonts/arial.ttf make sure that the name of the font must be same in the CustomTextView java file and to call the CustomTextView from xml you can use the following code:

<com.package.name.CustomTextView
//fill in the details
/>

Thanks

Upvotes: 0

Amir
Amir

Reputation: 391

You should pay attention to "fonts/bn.ttf". All the words must be in prototype.
May the bn.ttf be BN.TTF. So pay attention to assets folder and make sure from prototype name.

Upvotes: 0

Ali Allahyar
Ali Allahyar

Reputation: 341

I found the answer. As silwar pointed it was for the context for I hadn't assigned an activity with the content view of my XML layout. I was just using another activity which had a different content view.

But there is one question. Still the text view doesn't seem to apply the noted font. It just uses a default font. I have the noted XML layout for a list view which I want to use the typeface I have assigned. It adds the strings to an ArrayList and It the adapter for the list view uses the code as follows:

defListAdapter = new ArrayAdapter<String>(this, R.layout.def_list, list);
defListView.setAdapter(defListAdapter);

def_list is the XML layout which I pointed out in my previous question. defListView is the list view. Why don't I see the desired typeface?

Upvotes: 0

silwar
silwar

Reputation: 6518

Problem might be in

this.getAssets()

getAssets() is method from Context so try to pass context object instead of this

Try

Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/bn.ttf");

or

Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "fonts/bn.ttf");

Here mContext is object of Context

Edit :

Please check if following Custom TextView can help you

public class CustomTextView extends TextView{
  public CustomTextView(Context context,AttributeSet attrs,int defStyle){
    super(context,attrs,defStyle);
    init();
  }
  public CustomTextView(Context context,AttributeSet attrs){
    super(context,attrs);
    init();
  }
  public CustomTextView(Context context){
    super(context);
    init();
  }

  private void init(){
    if(!isInEditMode()){
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/bn.ttf");
        setTypeface(tf);
    }
  }
}

I have used this class in one of my previous project

Upvotes: 3

J Sanchez
J Sanchez

Reputation: 150

remove your typeface file from fonts/ and put it directly inside of Assets folder, should works! hope to help

Upvotes: 0

user2551221
user2551221

Reputation:

Your code, seems to be perfect Check out, font name is same as you have written in asset folder -> fonts folder. If same then try out by moving your fonts directly to asset folder and here just setting "bn.ttf" instead of "fonts/bn.ttf".

Upvotes: 0

Related Questions