Hello Friends, After searching hundreds of answers related to creating the customtextview or edittext I came up with my implementation to efficiently create custom view so that we don’t have to assign typeface from java code.
And I call it efficient because once I came to scenario related to creating multiple fragments inside FragmentStatePagerAdapter and I wasted days solving the problem of UI lagging and freezing.And then I came to know that the view is inflating the typeface in wrong way.
Contents
Top 10 free Android libraries for app development in android studio
Lets start to Create custom textview or edittext with custom fonts.
First is your custom view class.It may be EditText or Button also.
public class TextViewCustom extends TextView { private static final String TAG = TextViewCustom.class.getSimpleName(); public TextViewCustom(Context context) { super(context); } public TextViewCustom(Context context, AttributeSet attrs) { super(context, attrs); parseAttributes(context, attrs); } public TextViewCustom(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); parseAttributes(context, attrs); } private void parseAttributes(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomFont); String customFont = typedArray.getString(R.styleable.CustomFont_font); if (customFont != null && customFont.length() > 0) { setTypeface(TypeFaceProvider.getTypeFace(context, customFont)); } typedArray.recycle(); } }
Typeface Class to apply custom font to our view:
Be careful to put your font.ttf file inside main->assets->fonts->RobotoBold.ttf
public class TypeFaceProvider { public static final String TYPEFACE_FOLDER = "fonts"; public static final String TYPEFACE_EXTENSION = ".ttf"; private static Hashtable<String, Typeface> sTypeFaces = new Hashtable<String, Typeface>( 2); public static Typeface getTypeFace(Context context, String fileName) { Typeface tempTypeface = sTypeFaces.get(fileName); if (tempTypeface == null) { String fontPath = new StringBuilder(TYPEFACE_FOLDER).append('/').append(fileName).append(TYPEFACE_EXTENSION).toString(); tempTypeface = Typeface.createFromAsset(context.getAssets(), fontPath); sTypeFaces.put(fileName, tempTypeface); } return tempTypeface; } }
content of values–> attrs.xml (imp)
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomFont"> <attr name="font" format="string" /> </declare-styleable> </resources>
Usage:
Include the view in your xml file:
Take care of the app:CustomFont .This name matches the name set for the value we defin
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.test.askfortricks.TextViewCustom android:id="@+id/text_postItem" android:layout_width="wrap_content" android:layout_height="wrap_content" app:font="@string/font_opensans_bold" /> </LinearLayout>
Name of your font in values–> strings.xml file:
<!– Custom fonts text–>
<string name="font_opensans_bold">OpenSans-Bold</string>
You can get more idea about this custom view code from
https://github.com/askfortricks/CustomViewDemo
Ravi Yadav is an Android developer whose passion is to develop Android applications and sharing his work. He loves to post the tutorials which he created while learning any new latest technology topic.He loves to share about the latest technology happenings and other tricks that will be helpful for readers of Askfortricks.
Feeling glad to receive your comment