Search code examples
androidandroid-xmlandroid-custom-viewandroid-custom-attributes

Error with custom text-view attribute


I am using a custom textview with custom font as type face

my custom view is given below.

public class MyTextView extends TextView {

    public MyTextView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setType(context, defStyle);
    }

    public MalayalamTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setType(context, attrs.getAttributeIntValue(
                "http://schemas.android.com/apk/res/android", "textStyle", 0));
    }

    public MyTextView (Context context) {
        super(context);
        setType(context, Typeface.NORMAL);
    }

    private void setType(Context context, int Style) {
        if (Cls_Utils.GetMyFontTypeFace(context) != null) {
            this.setTypeface(Cls_Utils.GetMyFontTypeFace(context), Style);
        }

    }
}

My problem is that, when i give the text style as "bold" or "italic" in the XML, it shows an error in the graphic view in eclipse.

Exception raised during rendering: For input string: "bold"
Exception details are logged in Window > Show View > Error Log

java.lang.NumberFormatException: For input string: "bold"
    at java.lang.NumberFormatException.forInputString(    at java.lang.Long.parseLong(    at com.android.internal.util.XmlUtils_Delegate.convertValueToInt(XmlUtils_Delegate.java:72)
    at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:88)
    at android.util.BridgeXmlPullAttributes.getAttributeIntValue(BridgeXmlPullAttributes.java:134)
    at com.android.layoutlib.bridge.android.BridgeXmlBlockParser.getAttributeIntValue(BridgeXmlBlockParser.java:436)
    at com.kesariweekly.types.MalayalamTextView.<init>(MyTextView .java:19)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(    at sun.reflect.NativeConstructorAccessorImpl.newInstance(    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(    at java.lang.reflect.Constructor.newInstance(    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:438)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:161)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:132)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:806)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:385)

My XML Code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/imgArticle"
        android:layout_width="90dp"
        android:layout_height="130dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/mTvTitle"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/ic_not_available" />

    <com.myapp.types.MyTextView

        android:id="@+id/mTvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:gravity="center"
        android:text="തലക്കെട്ട്"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <com.myapp.types.MyTextView

        android:id="@+id/mTvAuthor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/mTvTitle"
        android:layout_below="@id/mTvTitle"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="7dp"
        android:layout_toRightOf="@+id/imgArticle"
        android:gravity="right"
        android:paddingRight="10dp"
        android:text="ലേഖകൻ"
        android:textColor="@color/black"
        android:textSize="15sp"
        android:textStyle="italic" />

    <com.myapp.types.MyTextView
        android:id="@+id/mTvSummary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tvReadLink"
        android:layout_below="@id/mTvAuthor"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="7dp"
        android:layout_toRightOf="@id/imgArticle"
        android:clickable="true"
        android:onClick="onClickRead"
        android:text="ചുരുക്കരൂപം" />

    <TextView
        android:id="@+id/tvReadLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/imgArticle"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp"
        android:clickable="true"
        android:onClick="onClickRead"
        android:text="Read >>"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/blue_dark" />

</RelativeLayout>

What to do?? My code is working and typeface is set correctly in emulator, but the only problem is this error message in the graphic view of xml.

Please help. sorry for my bad english.


Solution

  • If you only get the error in the xml graphics view, add this to youre code

     if(!isInEditMode())
             //use fonts
    

    The editor will display only default fonts.