0

I am working on an android project, the application is already built but just in order to improve the user interface. I am working on restyling the application. For that I am also trying to use a custom font, but that is not working. I have gone through the sample codes available on the web but none doesn't solve my problem. Here is how I am doing it.

    package com.crittermap.backcountrynavigator.dialog;

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() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/handsean.ttf");
            setTypeface(tf);
        }
    }

}

Here is the XML part

<com.crittermap.backcountrynavigator.dialog.MyTextView
            android:id="@+id/offlinesummary"
            android:layout_marginLeft="5dip"          
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@id/offlinecheckbox"
            android:text="@string/cp_preview_off_desc"
            style="@style/textN_l"> 
           </com.crittermap.backcountrynavigator.dialog.MyTextView>

Style :

 <style name="textN_l" parent="w_h">
    <item name="android:textColor">@android:color/black</item>
    <item name="android:textSize">12sp</item>
</style>  
 <style name="w_h"> <!-- Width and Height -->
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>        
</style>

And the errors I am getting are

    06-15 08:35:24.778: E/AndroidRuntime(2304): FATAL EXCEPTION: main
06-15 08:35:24.778: E/AndroidRuntime(2304): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.crittermap.backcountrynavigator.license/com.crittermap.backcountrynavigator.dialog.MapControlPanelDialog}: android.view.InflateException: Binary XML file line #24: Error inflating class com.crittermap.backcountrynavigator.dialog.MyTextView
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.os.Looper.loop(Looper.java:123)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at java.lang.reflect.Method.invokeNative(Native Method)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at java.lang.reflect.Method.invoke(Method.java:507)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at dalvik.system.NativeStart.main(Native Method)
06-15 08:35:24.778: E/AndroidRuntime(2304): Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class com.crittermap.backcountrynavigator.dialog.MyTextView
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.view.LayoutInflater.createView(LayoutInflater.java:518)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.app.Activity.setContentView(Activity.java:1657)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at com.crittermap.backcountrynavigator.dialog.MapControlPanelDialog.onCreate(MapControlPanelDialog.java:72)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-15 08:35:24.778: E/AndroidRuntime(2304):     ... 11 more
06-15 08:35:24.778: E/AndroidRuntime(2304): Caused by: java.lang.reflect.InvocationTargetException
06-15 08:35:24.778: E/AndroidRuntime(2304):     at java.lang.reflect.Constructor.constructNative(Native Method)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.view.LayoutInflater.createView(LayoutInflater.java:505)
06-15 08:35:24.778: E/AndroidRuntime(2304):     ... 22 more
06-15 08:35:24.778: E/AndroidRuntime(2304): Caused by: java.lang.RuntimeException: native typeface cannot be made
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.graphics.Typeface.<init>(Typeface.java:147)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at android.graphics.Typeface.createFromAsset(Typeface.java:121)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at com.crittermap.backcountrynavigator.dialog.MyTextView.init(MyTextView.java:27)
06-15 08:35:24.778: E/AndroidRuntime(2304):     at com.crittermap.backcountrynavigator.dialog.MyTextView.<init>(MyTextView.java:17)
06-15 08:35:24.778: E/AndroidRuntime(2304):     ... 25 more

Thank you

Rookie
  • 597
  • 3
  • 8
  • 18

5 Answers5

2

Did you forget the namespace?

<com.crittermap.backcountrynavigator.dialog.MyTextView
        android:id="@+id/offlinesummary"
        android:layout_marginLeft="5dip"          
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/offlinecheckbox"
        android:text="@string/cp_preview_off_desc"
        android:style="@style/textN_l"> 
</com.crittermap.backcountrynavigator.dialog.MyTextView>
user1417430
  • 376
  • 2
  • 6
  • What I meant was the `android:` namespace, but looking at your stacktrace, the problem seems to be in the custom font itself. Are you sure you have the "handsean.ttf" in the "fonts" folder within "assets"? – user1417430 Jun 26 '12 at 10:11
  • There is no way you can add style by 'android:style' Its style. Yes the font is there, in the assets folder and then in fonts folder. – Rookie Jun 26 '12 at 10:13
  • I would have posted it in comments. But I did not have enough reputation. Again, the stacktrace clearly mentions the error to be in the constructor of `TypeFace`. – user1417430 Jun 26 '12 at 10:22
  • Another note: the filename is case sensitive. So make sure the filename used in the code is exactly the same as the name of the actual file. – user1417430 Jun 26 '12 at 10:26
0

Try to add the layout width and height. Then, execute the program.

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
DeepakAndroid
  • 137
  • 1
  • 8
0

change your code as

public class MyTextView extends TextView {
 private Paint mPaint;  
float middleY=1;  
float mX=30;  
float DY=30; 
    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() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/handsean.ttf");
        mPaint = new Paint();  
        mPaint.setAntiAlias(true);  
        mPaint.setTextSize(16);  
        mPaint.setColor(Color.BLACK);  
        mPaint.setTypeface(tf); 
          //  setTypeface(tf);
        }
    }
    @Override  
    protected void onDraw(Canvas canvas) {  
        // TODO Auto-generated method stub  
        super.onDraw(canvas);  
        canvas.drawColor(0xEFeffff);  
        Paint p = mPaint;   
        p.setTextAlign(Paint.Align.CENTER);          
        canvas.drawText("TESTEST", mX, middleY, p);    

    } 
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • for more help you can view following code for setting custom fonts in TextView https://sites.google.com/site/imrankhanandroid/customtextview.txt?attredirects=0&d=1 – ρяσѕρєя K Jun 26 '12 at 10:27
  • @FatimaRizwan : you try https://sites.google.com/site/imrankhanandroid/customtextview.txt?attredirects=0&d=1 example – ρяσѕρєя K Jun 26 '12 at 10:38
0

try this

<com.crittermap.backcountrynavigator.dialog.MyTextView
    android:id="@+id/offlinesummary"
    android:layout_marginLeft="5dip" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"         
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@id/offlinecheckbox"
    android:text="@string/cp_preview_off_desc"
    android:style="@style/textN_l"> 

UPDATE

So other code is looks perfect. Only problem remains is in your ".ttf" file, may be this file is damaged. Download fonts from here developer.android.com/design/downloads/index.html and use them. hope it will work.

Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
  • Are you try this? I have tested it. It is working absolutely perfect. You need to give the android:layout_width="fill_parent" android:layout_height="wrap_content" you are not giving these parameters in your XML. – Mohsin Naeem Jun 26 '12 at 10:47
  • 1
    oh Sorry I don't see that. So other code is looks perfect. Only problem remains is in your ".ttf" file, may be this file is damaged. try to download fonts from hee http://developer.android.com/design/downloads/index.html and used them – Mohsin Naeem Jun 26 '12 at 11:06
0

u have to use in xml as given below

  <com.crittermap.backcountrynavigator.dialog.MyTextView
        android:id="@+id/offlinesummary"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"   
        android:layout_marginLeft="5dip"          
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/offlinecheckbox"
        android:text="@string/cp_preview_off_desc"
        >
  </com.crittermap.backcountrynavigator.dialog.MyTextView>

and in your any activity u have to use this way

MyTextView textview=(MyTextView)findViewById(R.id.offlinesummary);
Khan
  • 7,585
  • 3
  • 27
  • 44