2

I have tried everything I could find to get custom font working, but I keep getting a 'null pointer exception. Here's what I did:

  1. Created folder 'fonts' in 'assets' directory.

  2. Downloaded Google's 'Roboto' font from here: http://www.fontspace.com/google-android/roboto

  3. Renamed the file 'Roboto-Regular.ttf' to 'r.ttf'

  4. Uploaded to assets/fonts/ directory

  5. did Project->Clean

Now I have this code:

            try {
                fileList = am.list("fonts");

                 if (fileList != null)
                 {   
                     for ( int i = 0;i<fileList.length;i++)
                     {
                         Toast.makeText(getApplicationContext(), fileList[i] + "!", Toast.LENGTH_LONG).show();
                     }
                 }
            } catch (IOException e) {
                   Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
            }


         try {

         Typeface font = Typeface.createFromAsset(getAssets(), "fonts/r.ttf");

         TextView txt = (TextView) findViewById(R.id.rowTextView);
         txt.setTypeface(font);
         }
         catch(Exception e)
         {
             Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
         }

The first part of the code lists all items in the 'fonts' folder, I get a popup showing 'r.ttf'.

The second part of the code tries to load the font, I get a popup from there with 'null pointer exception'.

Any ideas what am I doing wrong? Thanks!

EDIT: The exception is thrown at by this: java.lang.NullPointerException on txt.setTypeface(font);

EDIT2:

This is my activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kb.klog.MainActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

</ListView>

</RelativeLayout>

This is the row I'm trying to change the font to: /res/layout/row.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"  
android:id="@+id/rowTextView"   
android:layout_width="fill_parent"   
android:layout_height="wrap_content"  
android:padding="5dp"  
android:textColor="#aaa"
android:textSize="18sp">  

user3578847
  • 397
  • 2
  • 6
  • 17
  • Are you sure you have that id, and it is already inflated at the point you try to access it? – dragi Oct 19 '14 at 13:52
  • I have that ID, I can hold ctrl+click_on_id and it takes me to the xml file. Not sure what you mean by 'already inflated at the point you try to access it', I have this code in 'onCreate' event – user3578847 Oct 19 '14 at 13:55
  • Do you have **setContentView** on the correct xml? – dragi Oct 19 '14 at 13:58
  • I have setContentView(R.layout.activity_main); Thanks! I updated my question with the layout code. – user3578847 Oct 19 '14 at 14:06

2 Answers2

2

The value of txt is null, because this row has not been inflated at that time. I would propose to use this answer https://stackoverflow.com/a/9035924/2160877
In general you should create your own class that extends TextView. Then you refer to that class in your xml and that's all.

In case the link is removed, here is the code for your class (as taken from the link, with some modifications for your case):

package com.yourPackageNameGoesHere;

import android.content.Context;
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();
    }

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

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

    private void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                                               "r.ttf");
        setTypeface(tf);
    }

}

Then your /res/layout/row.xml will look like:

<com.yourPackageNameGoesHere.MyTextView xmlns:android="http://schemas.android.com/apk/res/android"  
android:id="@+id/rowTextView"   
android:layout_width="fill_parent"   
android:layout_height="wrap_content"  
android:padding="5dp"  
android:textColor="#aaa"
android:textSize="18sp"> 
Community
  • 1
  • 1
dragi
  • 3,385
  • 5
  • 22
  • 40
  • Thanks a lot for the answer. I'm getting an Eclipse error: XML document structures must start and end within the same entity. I changed the name to my project. – user3578847 Oct 19 '14 at 14:30
0

if you need custom font just use this code

sure after you put , your font in assets/fonts

        Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
        TextView myTextView = (TextView)findViewById(R.id.myTextView);
        myTextView.setTypeface(myTypeface);
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156