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:
Created folder 'fonts' in 'assets' directory.
Downloaded Google's 'Roboto' font from here: http://www.fontspace.com/google-android/roboto
Renamed the file 'Roboto-Regular.ttf' to 'r.ttf'
Uploaded to assets/fonts/ directory
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">