1

I'm trying to put a custom font in my app, but I'm having a lot of mistakes! Someone please help me? I am a student and do not have much sense.

This is the code I'm trying to put inside MainActivity.java:

TextView tx = (TextView)findViewById(R.id.textView1);
Typeface custom_font = Typeface.createFromAsset(this.getAssets(),
    "fonts/watermelon.ttf");
tx.setTypeface(custom_font);

This is the code of MainActivity:

package com.akzonobel.malote;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.method.ScrollingMovementMethod;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.os.Build;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.widget.Button;
import android.widget.TextView;

@SuppressWarnings("unused")
public class MainActivity extends ActionBarActivity{


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

        Typefaces.get(getApplication(), "fonts/watermelon.ttf");



TextView tx = (TextView)findViewById(R.id.textView1);
Typeface custom_font = Typeface.createFromAsset(this.getAssets(),
    "fonts/watermelon.ttf");
tx.setTypeface(custom_font);


        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();}
                    }

    public void opensecondactivity(View view) {
        Intent intent = new Intent(this , SecondActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
           } 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = new Intent(this , AboutActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
              }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView; 
            }
          }
       }

With this code the Eclipse does not declare any errors, but when he runs he Activity of error and application closes alone. What to do?

Debug:

New_configuration [Android Application] 
    DalvikVM [localhost:8612]   
        Thread [<1> main] (Suspended (exception RuntimeException))  
            <VM does not provide monitor information>   
            ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2195    
            ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2245 
            ActivityThread.access$800(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 135    
            ActivityThread$H.handleMessage(Message) line: 1196  
            ActivityThread$H(Handler).dispatchMessage(Message) line: 102    
            Looper.loop() line: 136 
            ActivityThread.main(String[]) line: 5017    
            Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
            Method.invoke(Object, Object...) line: 515  
            ZygoteInit$MethodAndArgsCaller.run() line: 779  
            ZygoteInit.main(String[]) line: 595 
            NativeStart.main(String[]) line: not available [native method]  
        Thread [<10> Binder_2] (Running)    
        Thread [<9> Binder_1] (Running) 
        Thread [<11> Timer-0] (Running) 

Logcat:

06-14 09:58:59.484: W/ActivityThread(1243): Application com.akzonobel.malote is waiting for the debugger on port 8100...
06-14 09:58:59.494: I/System.out(1243): Sending WAIT chunk
06-14 09:58:59.584: I/dalvikvm(1243): Debugger is active
06-14 09:58:59.694: I/System.out(1243): Debugger has connected
06-14 09:58:59.744: I/System.out(1243): waiting for debugger to settle...
06-14 09:58:59.954: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:00.154: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:00.354: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:00.564: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:00.764: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:00.994: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:01.194: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:01.394: I/System.out(1243): waiting for debugger to settle...
06-14 09:59:01.594: I/System.out(1243): debugger has settled (1348)
06-14 09:59:02.424: D/(1243): HostConnection::get() New Host Connection established 0xb8f54e48, tid 1243
06-14 09:59:02.514: W/EGL_emulation(1243): eglSurfaceAttrib not implemented
06-14 09:59:02.524: D/OpenGLRenderer(1243): Enabling debug mode 0
06-14 09:59:03.054: W/EGL_emulation(1243): eglSurfaceAttrib not implemented
06-14 09:59:06.384: D/AndroidRuntime(1243): Shutting down VM
06-14 09:59:06.384: W/dalvikvm(1243): threadid=1: thread exiting with uncaught exception (group=0xb1aa8ba8)
06-14 09:59:06.484: E/AndroidRuntime(1243): FATAL EXCEPTION: main
06-14 09:59:06.484: E/AndroidRuntime(1243): Process: com.akzonobel.malote, PID: 1243
06-14 09:59:06.484: E/AndroidRuntime(1243): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.akzonobel.malote/com.akzonobel.malote.MainActivity}: java.lang.RuntimeException: native typeface cannot be made
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.os.Looper.loop(Looper.java:136)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at java.lang.reflect.Method.invokeNative(Native Method)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at java.lang.reflect.Method.invoke(Method.java:515)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at dalvik.system.NativeStart.main(Native Method)
06-14 09:59:06.484: E/AndroidRuntime(1243): Caused by: java.lang.RuntimeException: native typeface cannot be made
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.graphics.Typeface.<init>(Typeface.java:175)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.graphics.Typeface.createFromAsset(Typeface.java:149)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at com.akzonobel.malote.MainActivity.onCreate(MainActivity.java:32)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.Activity.performCreate(Activity.java:5231)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-14 09:59:06.484: E/AndroidRuntime(1243):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-14 09:59:06.484: E/AndroidRuntime(1243):     ... 11 more
06-14 10:00:38.184: I/Process(1243): Sending signal. PID: 1243 SIG: 9

The source is in the right folder (assets / fonts) and font name and its extension are all lowercase.

Please explain as detailed as possible because I am a high school student and am new to programming.

Sorry for my bad english.

Thanks!

UPDATE ! - LOGCAT:

06-14 18:31:33.607: D/(2031): HostConnection::get() New Host Connection established 0xb837bfa8, tid 2031
06-14 18:31:33.677: W/EGL_emulation(2031): eglSurfaceAttrib not implemented
06-14 18:31:33.687: D/OpenGLRenderer(2031): Enabling debug mode 0
06-14 18:31:34.037: W/EGL_emulation(2031): eglSurfaceAttrib not implemented
06-14 18:31:37.197: E/Typefaces(2031): Could not get typeface 'fonts/watermelon.ttf' because native typeface cannot be made
06-14 18:31:49.457: D/AndroidRuntime(2031): Shutting down VM
06-14 18:31:49.467: W/dalvikvm(2031): threadid=1: thread exiting with uncaught exception (group=0xb1a42ba8)
06-14 18:31:49.557: E/AndroidRuntime(2031): FATAL EXCEPTION: main
06-14 18:31:49.557: E/AndroidRuntime(2031): Process: com.akzonobel.malote, PID: 2031
06-14 18:31:49.557: E/AndroidRuntime(2031): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.akzonobel.malote/com.akzonobel.malote.MainActivity}: java.lang.NullPointerException
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.os.Looper.loop(Looper.java:136)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at java.lang.reflect.Method.invokeNative(Native Method)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at java.lang.reflect.Method.invoke(Method.java:515)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at dalvik.system.NativeStart.main(Native Method)
06-14 18:31:49.557: E/AndroidRuntime(2031): Caused by: java.lang.NullPointerException
06-14 18:31:49.557: E/AndroidRuntime(2031):     at com.akzonobel.malote.MainActivity.onCreate(MainActivity.java:35)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.Activity.performCreate(Activity.java:5231)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-14 18:31:49.557: E/AndroidRuntime(2031):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-14 18:31:49.557: E/AndroidRuntime(2031):     ... 11 more
Cœur
  • 37,241
  • 25
  • 195
  • 267
Renan
  • 43
  • 1
  • 8
  • possible duplicate of [Can I embed a custom font in an Android application?](http://stackoverflow.com/questions/3424165/can-i-embed-a-custom-font-in-an-android-application) – metacubed Jun 14 '14 at 20:10
  • @metacubed No, in my case nothing I do is working to implement the source – Renan Jun 14 '14 at 20:16
  • Why are you writing Typefaces.get()? I don't think they are needed. – berserk Jun 14 '14 at 20:32
  • Oh, it was a test I was doing according to [this](http://stackoverflow.com/questions/12766930/native-typeface-cannot-be-made-only-for-some-people) solution, but without result. – Renan Jun 14 '14 at 20:35
  • Have you tried removing it? – berserk Jun 14 '14 at 20:35
  • Yes, but it still does not work – Renan Jun 14 '14 at 20:37
  • I guess you have to put the watermelon.ttf file directly in the assets folder then. – berserk Jun 14 '14 at 20:41
  • try this: http://stackoverflow.com/a/13087128/2944070 – berserk Jun 14 '14 at 20:55
  • I was trying to implement this code, but did not quite understand ... You know how to use this tutorial? Excuse my "stupidity" – Renan Jun 14 '14 at 21:04
  • I guess you have to use that class Typefaces and use its function get() i.e. Typefaces.get(this, path); It will return Typeface – berserk Jun 14 '14 at 21:14
  • @bersek I tried `Typefaces.get(this, "fonts/watermelon.ttf");`. Resolve the error of the application, it opens normally ... but the source does not work (does not apply) – Renan Jun 14 '14 at 21:48
  • You have do do like tx.setTypeface(Typefaces.get(this, "fonts/watermelon.ttf")); – berserk Jun 14 '14 at 21:53
  • @bersek Would look like? `TextView tx = (TextView)findViewById(R.id.texto); tx.setTypeface(Typefaces.get(this, "fonts/watermelon.ttf"));` – Renan Jun 14 '14 at 22:01
  • @bersek Unfortunately continues giving error. `06-14 17:34:35.497: E/Typefaces(1196): Could not get typeface 'fonts/watermelon' because native typeface cannot be made` – Renan Jun 14 '14 at 22:13
  • @bersek I checked if it is in the right folder, I've tried putting the source in main assets. I tried to source type ".otf" and tried another source. Nothing works. :( – Renan Jun 14 '14 at 22:14

3 Answers3

1

Have you tried removing this from this.getAssets():

TextView tx = (TextView)findViewById(R.id.textView1);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/watermelon.ttf");
tx.setTypeface(custom_font);

According to this answer that should fix it.

Community
  • 1
  • 1
eikooc
  • 2,363
  • 28
  • 37
  • In your layout xml for textView1 have you added the custom font as according to [this tutorial](http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts/) – eikooc Jun 14 '14 at 20:19
  • I checked several times, as is everything this tutorial. I've tried other tutorials too, and still does not work, the application closes alone – Renan Jun 14 '14 at 20:23
  • In which folder have you put your font file? It should be placed in assets/fonts/ – eikooc Jun 14 '14 at 20:24
  • She is in `assets/fonts` – Renan Jun 14 '14 at 20:31
  • Try swapping the Typeface definition around with the textview, and have you remembered to add it as a ressource in xml? – eikooc Jun 14 '14 at 20:41
  • Yes,I've tried everything, the same error persists. – Renan Jun 14 '14 at 20:50
0

Solution by OP.

The solution was to implement the code within the public view onCreateView. See how it fits:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    TextView txt = (TextView) rootView.findViewById(R.id.TextView1);
    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/laouib.TTF");
    txt.setTypeface(font); 

    return rootView;
Cœur
  • 37,241
  • 25
  • 195
  • 267
0

Add youre font into src/main/assets/fonts and in youre Application class add this: TypefaceUtils.overrideFont(getApplicationContext(), "SERIF", "nameofyourefont.ttf");

Eder Padilla
  • 1,507
  • 1
  • 13
  • 22