Thursday, 1 January 2015

Using extended EditText (or TextView) in the layout xml



i have the following code to add a custom font for my EditText as well as TextView


package com.usjp.xxx.yyyy;



import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;

public class CustomEditTextWithFont extends EditText {
public CustomEditTextWithFont(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

public CustomEditTextWithFont(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public CustomEditTextWithFont(Context context) {
super(context);
init();
}

public void init() {
Typeface tf = Typeface.createFromAsset((getContext().getAssets()),"CaviarDreams_Bold.ttf");
setTypeface(tf ,1);

}
}


and in my layout xml when i have



<com.usjp.xxx.yyyy.util.CustomEditTextWithFont
xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/ET_password1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_above="@+id/BtnLogin"
android:textColor="#666362"
android:layout_alignLeft="@+id/ET_login"
android:layout_marginBottom="31dp"
android:background="@drawable/edit_text_design"
android:ems="10"
android:hint="@string/password"
android:inputType="textPassword"/>


but then when running it says



Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class com.usjp.xxx.yyyy.util.CustomEditTextWithFont


can someone please tell me what i'm doing wrong here? Thanks a lot in advance :)


No comments:

Post a Comment