Skip to main content

How to change the font on the TextView?

   How to change the font on the TextView?


A1. 

                          First download the .ttf file of the font you need (arial.ttf). Place it in the                                     assets folder(Inside assets folder create new folder named fonts and place it                                 inside it). If  txtyour is the textviews you want to apply the font , use the                                       following piece of code,


      Typeface type = Typeface.createFromAsset(getAssets(),"fonts/Kokila.ttf"); 
      txtyour.setTypeface(type);



A2.


     Typeface tf = Typeface.createFromAsset(getAssets(),
        "fonts/DroidSansFallback.ttf");
    TextView tv = (TextView) findViewById(R.id.CustomFontText);
    tv.setTypeface(tf);




A3.


       
    public class Font {
    public static final Font  PROXIMA_NOVA    = new Font("ProximaNovaRegular.otf");
    public static final Font  FRANKLIN_GOTHIC = new Font("FranklinGothicURWBoo.ttf");
    private final String      assetName;
    private volatile Typeface typeface;

    private Font(String assetName) {
    this.assetName = assetName;
    }

    public void apply(Context context, TextView textView) {
    if (typeface == null) {
      synchronized (this) {
        if (typeface == null) {
          typeface = Typeface.createFromAsset(context.getAssets(), assetName);
        }
      }
    }
    textView.setTypeface(typeface);
   }
   }


And then to use in your activity...

    myTextView = (TextView) findViewById(R.id.myTextView);
    Font.PROXIMA_NOVA.apply(this, myTextView);














Comments

Popular posts from this blog

android ellipsize multiline textview ?

android ellipsize multiline textview ? A1. XML         android : maxLines = "2" android : ellipsize = "end" android : singleLine = "false" A2.        Try this, it works for me, I have 4 lines and it adds the "..." to the end of the last/fourth line.        Its  the same as morale's answer but i have singeLine="false" in there. <TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:maxLines = "4" android:ellipsize = "marquee" android:singleLine = "false" android:text = "Hi make this a very long string that wraps at least 4 lines, seriously make it really A3.        There are a few attributes you should check: android:lines, android:minLines,                             android:maxLines. To display a maximum of 4 lines and ellipsiz...

Reset Android textview maxlines ?

Reset Android textview maxlines  ? A1.         Actually, the way android platform does that is by setting the MaxLine to                                            Integer.MAX_VALUE. textView . setMaxLines ( Integer . MAX_VALUE );        also, if you are using Ellipsize, don't forget to set to null. textView . setEllipsize ( null );       just check how the android framework do just that ;) watch the                                                              setMaxLines(Integer.MAX_VALUE); private void applySingleLine ( boolean singleLine , boolean applyTransformation ) { mSingleLine = singleLine ; if ( singleLine ) { ...

Android set the gravity for a TextView programmatically

Android set the gravity for a TextView programmatically  A1.        This will center the text in a text view: TextView ta = ( TextView ) findViewById ( R . layout . text_view ); LayoutParams lp = new LayoutParams (); lp . gravity = Gravity . CENTER_HORIZONTAL ; ta . setLayoutParams ( lp ); A2.      textView . setGravity ( Gravity . CENTER | Gravity . BOTTOM ); A3.              Use this code TextView textView = new TextView ( YourActivity . this ); textView . setGravity ( Gravity . CENTER | Gravity . TOP ); textView . setText ( "some text