Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This might be a little late, but you need to create a singleton class that returns the custom typeface to avoid memory leaks.</p> <p>TypeFace class:</p> <pre><code>public class OpenSans { private static OpenSans instance; private static Typeface typeface; public static OpenSans getInstance(Context context) { synchronized (OpenSans.class) { if (instance == null) { instance = new OpenSans(); typeface = Typeface.createFromAsset(context.getResources().getAssets(), "open_sans.ttf"); } return instance; } } public Typeface getTypeFace() { return typeface; } } </code></pre> <p>Custom TextView:</p> <pre><code>public class NativelyCustomTextView extends TextView { public NativelyCustomTextView(Context context) { super(context); setTypeface(OpenSans.getInstance(context).getTypeFace()); } public NativelyCustomTextView(Context context, AttributeSet attrs) { super(context, attrs); setTypeface(OpenSans.getInstance(context).getTypeFace()); } public NativelyCustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setTypeface(OpenSans.getInstance(context).getTypeFace()); } } </code></pre> <p>By xml:</p> <pre><code>&lt;com.yourpackage.views.NativelyCustomTextView android:id="@+id/natively_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_margin="20dp" android:text="@string/natively" android:textSize="30sp" /&gt; </code></pre> <p>Programmatically:</p> <pre><code>TextView programmaticallyTextView = (TextView) findViewById(R.id.programmatically_text_view); programmaticallyTextView.setTypeface(OpenSans.getInstance(this) .getTypeFace()); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload