Nitesh Tiwari
Nitesh Tiwari

Reputation: 4762

Null Pointer in Typeface.createFromAsset()

My Folder Structure

Folder Structure

Code for font:

Typeface typeface = Typeface.createFromAsset(this.getAssets() , AppContants.ROBOT_MEDIUM);

My Source set:

 sourceSets {
        main { java.srcDirs = ['src/main/java', 'src/main/res/xml']
            assets.srcDirs = ['src/main/java/assets']
        }
    }

Getting Null pointer for this Typeface instance.

Upvotes: 0

Views: 1115

Answers (2)

chain
chain

Reputation: 659

Add fonts folder to your path. Move your .ttf files to the fonts folder.

 sourceSets {
    main { java.srcDirs = ['src/main/java', 'src/main/res/xml']
        assets.srcDirs = ['src/main/java/assets/fonts']
    }
}

Upvotes: 2

gsanskar
gsanskar

Reputation: 669

Put your font file in assets/fonts directory an then, Simply use :

Typeface tf = Typeface.createFromAsset(getBaseContext().getAssets(),
                fontPath);

where fontPath is the path to your font file in assets folder.

fontPath = "fonts/<your_font_file_name>"

Hope this Helps!

Upvotes: 1

Related Questions